1

While I was going through one project I found some strange variables names. Sonarqube gaves me Code Smell statements about these variables.

For example protected String value$editedby$java$lang$String;

And message from Sonarqube:

Rename this field "value$editedby$java$lang$String" to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

So is it proper way to use $ in variables names?

uncleMatuesz
  • 87
  • 1
  • 1
  • 10
  • 1
    Related: http://stackoverflow.com/questions/7484210/what-is-the-meaning-of-in-a-variable-name – Maroun Jan 02 '17 at 15:34
  • You might wanna ask this question on Code Review network rather than StackOverflow... – radoh Jan 02 '17 at 15:35
  • 1
    [http://stackoverflow.com/a/7484245/348975](http://stackoverflow.com/a/7484245/348975) explains why you should not use `$`. It is largely bullshit in my opinion - if you are not supposed to use `$` why is it an allowed character. On the other hand there is no strong compelling reason to use `$` and it is commonly accepted BS - so I follow along. – emory Jan 02 '17 at 15:35

3 Answers3

4

I would strongly recommend against it, JLS-3.8. Identifiers says (in part)

The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

While it's technically legal to use the $ character in variable names, Java's coding standard discourages this. The usual convention for variables is lowerCaseCamelCase. So, in your case:

protected String valueEditedByJavaLangString;
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Camel case is recommended with a lowercase first letter. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

I would add that the $ character is traditionally used by the compiler in the name of generated class file for inner class.

davidxxx
  • 125,838
  • 23
  • 214
  • 215