Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using underscores in Java variables and method names [closed]

Even nowadays I often see underscores in Java variables and methods, an example are member variables (like "m_count" or "_count"). As far as I remember, to use underscores in these cases is called bad style by Sun.

The only place they should be used is in constants (like in "public final static int IS_OKAY = 1;"), because constants should be all upper case and not camel case. Here, the underscore should make the code more readable.

Do you think using underscores in Java is bad style? If so (or not), why?

like image 474
Georgi Avatar asked Sep 29 '08 19:09

Georgi


People also ask

Can I use underscore in Java method name?

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed. Variable names should be short yet meaningful.

Can method name have underscore?

A single leading underscore in front of a variable, a function, or a method name means that these objects are used internally. This is more of a syntax hint to the programmer and is not enforced by the Python interpreter which means that these objects can still be accessed in one way on another from another script.

Are underscores allowed in variable names?

Variable names should not start with underscore ( _ ) or dollar sign ( $ ) characters, even though both are allowed. This is in contrast to other coding conventions that state that underscores should be used to prefix all instance variables. Variable names should be short yet meaningful.

Is underscore valid for variable in Java?

Using underscore in a variable like first_name is still valid. But using _ alone as a variable name is no more valid. Even if you are using earlier versions of Java, using only underscore as a variable name is just a plain bad style of programming and must be avoided.


1 Answers

If you have no code using it now, I'd suggest continuing that. If your codebase uses it, continue that.

The biggest thing about coding style is consistency. If you have nothing to be consistent with, then the language vendor's recommendations are likely a good place to start.

like image 127
Tanktalus Avatar answered Sep 20 '22 18:09

Tanktalus