When to use _ or $ conventions in Java identifiers?

1

When to use _ or $ in the identifiers? That is, in which cases should I use _ or $ to make my code more organized. For example, should I use $ in front of variable names when constructing a framework ?

    
asked by anonymous 14.09.2016 / 04:31

1 answer

2

In general, never.

$ is considered for internal use and only the compiler should generate identifiers with this character. Of course, exceptions can always be made if you have a good reason. Some tool that generates code may be able to use this to ensure that the identifiers it creates do not conflict with what the programmer uses. One more reason for the programmer not to use it in normal code. It's very rare for anyone to need it.

If you use this convention in a framework you're probably doing something wrong. But without a concrete case it is difficult to say.

_ has no specific use and can be used when you feel you should. But the recommendation is not to use it. It's a matter of style, Java avoids its use. If you want to identify it as a new word, just use upper case (camelCase). Exception is used when the identifier is a constant. As Java adopts the old convention of constant being ALL_CAPS the underscore ends up being necessary to separate words.

Some people like to use underscore at the beginning of the name in some situations, such as in private variables. I do not see need or gain.

The important thing is to adopt a pattern and follow it always.

    
14.09.2016 / 04:59