26

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?

Oliver Matthews
  • 7,497
  • 3
  • 33
  • 36
Ashok Koyi
  • 5,327
  • 8
  • 41
  • 50
  • 9
    Disagree with the close - Java is one of the languages which has an official style defined, so a question regarding whether the standard addresses a point seems reasonable. – Oliver Matthews Jun 18 '14 at 09:45

10 Answers10

43

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

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Can't imagine why this answer, and only this answer, was voted down five years later. – duffymo Jan 03 '15 at 00:09
  • Just voted up for fixing the justice. Edit: I'm not the guy who voted down – Denis Itskovich Jan 04 '15 at 21:14
  • and because your answer is the most useful one for me, I'll vote up – amal50 Nov 09 '15 at 23:54
  • 3
    Just because it's "pre-IDE and C++ vintage", we won't do it? IDE's give the advantage but it won't help me from renaming stuff when a private member gives conflict with a local variable or a method, especially when it comes to writing properties. (Downvoting because no technical/practical reason was mentioned.) – konsolebox Oct 05 '16 at 07:24
  • 2
    That's your choice. Did you downvote every other person who said they hate it, or did you single me out? You're seven years late to the party. I don't need such a convention to see that I have a name conflict that'll be a problem. Besides, that's trivially easy to sort out: Follow the idiom and distinguish the private member with "this". Your reasoning is...odd. You haven't answered a question here in almost two years and you return to comment on this? – duffymo Oct 05 '16 at 11:56
30

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
  • 73,784
  • 33
  • 194
  • 347
17

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
  • 14,540
  • 3
  • 37
  • 44
5

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 IV
  • 19,322
  • 3
  • 53
  • 57
  • 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 Dec 14 '09 at 08:45
  • Ahh, right, I think I would put the underscore at the end though. But that's a matter of taste I suppose. – Francis Upton IV Dec 14 '09 at 08:48
  • 3
    _ 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 Dec 14 '09 at 10:13
  • 3
    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 Dec 14 '09 at 10:15
  • If we use _ for names, wouldn't it be tough for us to create setter and getters in eclipse?? – Ashok Koyi Dec 14 '09 at 10:48
  • @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 IV Dec 14 '09 at 10:54
  • 3
    My main reason for using a leading `_` in field names is to avoid name clashes with local variables, without the need to think up new names or remember to prefix with `this.`. I also find it less obtrusive than prefixes such as `m` (Android) or even `m_` (I've used that at work, it was the coding style there). – starblue Dec 13 '10 at 14:49
  • My subconscious once found a bad casing of a constant, as it had the wrong color in my IDE. – Thorbjørn Ravn Andersen Nov 10 '15 at 08:24
3

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
  • 60,850
  • 18
  • 118
  • 163
1

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.

imdiva
  • 11
  • 1
1

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.

Pradeep
  • 19
  • 1
0

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
  • 528
  • 1
  • 5
  • 12
0
  1. Do not use _ when you use a modern LCD color monitor, with modern IDE like Eclipse, Netbeans, IntelliJ, Android Studio.
  2. Use _ when you need to use a old-style monochrome CRT monitor with only green or white text, or when you have difficulty to identify the color on LCD monitor.
  3. The consensus of your code collaborators overrides the above.
etoricky
  • 631
  • 1
  • 7
  • 10
0

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.

Michael Hegner
  • 5,555
  • 9
  • 38
  • 64