Why is it bad to start a variable name with a doll

2019-05-10 17:43发布

Why is it bad to start a variable name with a dollar sign in C++/Java and similar such as in PHP?

Edit: Are there any risks?

8条回答
爷、活的狠高调
2楼-- · 2019-05-10 18:24

For java from this post

The convention, however, is to always begin your variable names with a letter, not "$" or "_". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged.

I don't think there are any risks/side effects; it's just discouraged.

查看更多
干净又极端
3楼-- · 2019-05-10 18:28

In Java $ is used in inner class names, and probably some "synthetic" method names as well. Basically any code that the compiler has to generate to handle inner classes will have a $ in it. Using the $ would, at the very least be confusing because of that. You should, generally speaking, never need to see a $ in a variables/method/class name in Java.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-05-10 18:33

In Java, using $ in variables is legal but definitely a bad idea.

If you do this, there is a risk that you will accidentally use a name that collides with a name that is used by the compiler itself, or by some code generator. The result will be unexpected compile or runtime failures that could be particularly difficult to diagnose ...

There's also a potential risk that your (mis-)use of $ will cause problems in future versions of Java. The Java compiler / runtime's use of $ may change in a future, causing your abusive code to fail.

Just don't do it. Or at least, don't do it unless you are writing a generator ... and you know what you are getting yourself into.

查看更多
聊天终结者
5楼-- · 2019-05-10 18:33

Because the JLS 8 3.8 says so:

The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

And you can really generate code that fails to compile, e.g. on Oracle JDK 1.8.0_45:

public class Assert {
    static final boolean $assertionsDisabled = false;
    public static void main(String[] args) {
        assert System.currentTimeMillis() == 0L;
    }
}

The problem is that javac generates a field $assertionsDisabled to implement assert, which conflicts with our field.

See also: https://stackoverflow.com/a/29439538/895245

查看更多
啃猪蹄的小仙女
6楼-- · 2019-05-10 18:35

In C++, it's non-portable. The only characters the standard says (section [lex.name]) must be acceptable to begin an identifier are uppercase and lowercase letters and underscore.

查看更多
Bombasti
7楼-- · 2019-05-10 18:40

The main reasons I can think of are here. In Java, the inner class names are created with $ sign. Also, the $ sign is used for env variables in most of the Oses, so it would be confusing and may intervene the system defined variables.

查看更多
登录 后发表回答