开发者

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

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 r开发者_C百科isks?


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.


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.


Java allows the dollar sign to begin identifiers, but recommends that it only be used for mechanically generated code. (see here)

It appears that in C++ identifiers may be able to use dollar signs as an extension, but it's not part of the standard.


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.


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.


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


Because in both C++ and Java doing so would be a syntax error (in the case of C++) or bad style (in the case of Java). Neither of these languages need variables to be so indicated, and doing so is wrong. Starting variables with a special character (like '$') makes things a lot easier for the language system (which is important for interpreted languages like PHP), but makes life much harder for the programmer, because they have to type in all those '$' characters. So most languages (even most interpreted languages) don't require them.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜