Reputation: 5387
I have seen some programmers using _
(underscore) in front of class names, and others using it for local variables.
Does the Java standard require/suggest the use of _ (underscore) in front of an private instance variable or class name?
Upvotes: 27
Views: 45736
Reputation: 5823
I am using _
in front of class name, if the class is package private. So in the IDE I have grouped all package private classes on top of the package and I see immediately, that these classes are not accessable from outside of the package.
Upvotes: 0
Reputation: 691
Upvotes: 0
Reputation: 11
It seems to be a convention that states if an underscore is used as an initial character in the name of method, the method cannot be overridden.
For example, in context of JSP, _jspService() method cannot be overridden.
However, i dont find any document that states above convention.
Upvotes: 1
Reputation: 75346
It is a matter of personal taste.
Martin Fowler appears to like it. I don't care much for it - it breaks the reading pattern, and the information it conveys is already subtly told by color in your IDE.
I've experimented with using _
as the name of the variable holding the result to be returned by a method. It is very concise, but I've ended up thinking that the name result
is better.
So, get your colleagues to agree on what you all like the most and then just do that.
Upvotes: 30
Reputation: 19
Using _ before a variable helps in one case where if the developer wants to use a standard/key word as a variable name knowingly or unknowingly. It would not give a conflict if that has an additional character.
Upvotes: 1
Reputation: 528
I agree with the others here... It breaks the standard and doesn't really buy you anything if you just use a naming standard. If you need to know the difference between your class and local variables, try a naming convention like lVariableName where the l is used to indicate that it is local. To me this is unnecessary since the IDE highlighting is sufficient, but might be useful to some.
Upvotes: 0
Reputation: 308733
I think artifacts like these are pre-IDE and C++ vintage. Don't do it.
Upvotes: 43
Reputation: 61516
A lot of people is it for instance variables as it makes them stand out. Personally I hate it mainly because methods should, for the most part, be no more than 10 lines of code (I hardly ever go over 20). If you cannot see that a variable isn't local in under 10 lines then there is something wrong :-)
If you have 100 line methods and you don't declare all your variables at the top of the block that they are used in I can see why you would want to use _ to distinguish them... of course, as with most things, if it hurts then stop doing it!
Upvotes: 3
Reputation: 19443
Many people (including myself) use _ in front of field names. The reason for this is to easily distinguish them from local variables. However in the ages of IDEs this is not so necessary since syntax highlighting shows this. Using an underscore in front of a class name is just wrong. By convention, class names start with an uppercase letter.
Upvotes: 5
Reputation: 14738
if you want to follow best practice java , use the code convention outlined here http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
I.e., no underscore for private member variable names.
Tho, personally, i m not fussed either way, as long as consistency is applied.
Upvotes: 17