Conventions of when to use _ or $ in Java identifiers?

Asked

Viewed 51 times

1

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

1 answer

3

In a general way never at least one of them.

$ is considered to be of 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 use this to ensure that the identifiers created by it do not conflict with what the programmer uses. Another reason why the programmer does not use it in normal code. It is very rare who needs it.

If you use this convention on a framework is probably doing something wrong. But without a concrete case it is difficult to state.

_ has no specific use and can be used when you think you should. However the recommendation is not to use. It is a matter of style, Java avoids its use. If you want to identify that it is a new word just use uppercase (camelCase). An exception is used when the identifier is a constant. How Java adopts the old constant convention being ALL_CAPS the underscore ends up being necessary to separate words.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.