views:

362

answers:

6

I have seen some of our colleagues using _ [underscore] in front of class names [entities in JPA], and few more people using it for local variables.

Is there a standard[or atleast best practice] in java to use _ [underscore] in front of instance[private] instance variable or class name????

I have looked at the following link in SO [Stack Overflow]. I could not get my question answered. Using underscores in Java variables and method names

+11  A: 

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.

Chii
+3  A: 

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.

Francis Upton
There are occassions to use a _ in front of a class name, when you are worried that your class name might collide with another class name, and you want to distinguish your class from others. For instance purely internal classes often use this convention to hold things like Record, Data, etc which are common class names, so instead they choose _Record, _Data, etc.
GrayWizardx
Ahh, right, I think I would put the underscore at the end though. But that's a matter of taste I suppose.
Francis Upton
+1 for the "In the ages IDEs" + "syntax highlighting"
pimpf0r
_ in front of field names is ugly enough. Using it in Class names is something I wouldn't accept from any developer. There is a Syntax to difference between two classes with the same name, especially inner classes!
Hardcoded
You don't need to worry about collisions because everything goes by it's fully qualified name internally. Externally if you have a Record class that needs to reference another record class you can just type out 'whatever.other.package.Record' which can be unwieldy but it's not something that comes up to often.
Chad Okere
If we use _ for names, wouldn't it be tough for us to create setter and getters in eclipse??
Kalinga
@Kalinga, no it works fine in Eclipse, there is a preference to ignore the leading portion of the field name when generating the get/set methods.
Francis Upton
+1  A: 

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!

TofuBeer
+2  A: 

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.

Thorbjørn Ravn Andersen
+5  A: 

I think artifacts like these are pre-IDE and C++ vintage. Don't do it.

duffymo
A: 

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.

PaulP1975