1

Before forming a class in Java or other programming languages that support OOP, should I use underscore (_) in each (local or private) field declaration.

More precisely:

private String _customername;

Is this a correct declaration?

Hakan
  • 113
  • 1
  • 4
  • 2
    It depends on the language you use and on local conventions. For example, in C++ a leading underscore is in many cases reserved for use by the compiler/standard library, while in other languages it is used to designate a private member. – Bart van Ingen Schenau Feb 04 '14 at 09:14
  • 1
    1) This is unrelated to OOP. 2) If this is legal depends on the language. In Java, C# it's legal. In some other languages it might not be. 3) If it's a good idea depends on language conventions and personal preference. – CodesInChaos Feb 04 '14 at 10:05
  • Hmm.. normally before posting it, I checked the forum. I have overlooked the relating topic, though. Thanks all! – Hakan Feb 04 '14 at 13:48

3 Answers3

6

There is a strong convention in java circles of not using _ for member names (as opposed to C++ circles, where there is a strong convention for using them). This is largely an arbitrary difference that probably has more to do with the desire to reinforce a sense of community than with measurable advantages. It is usually a good idea to go along with a convention simply because it removes one barrier for other people to understand your code quickly.

However, the necessity of marking member variables at all is questionable. In my view, if your methods are so long that a reader has trouble telling local vars from member vars, then they are too long to begin with, and you should refactor them to be smaller and more self-contained rather than disambiguate via naming conventions.

Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
  • 2
    As a primarily-C# developer, I would say that it's not always about readability within methods (although that can be useful). It's about being able to think "what services do I have available in this class?" hit underscore and intellisense tells you. However, standards are more important than that handy feature, so +1. – pdr Feb 04 '14 at 10:02
3

I think this is a matter of personal preference. I use underscores on private fields because I feel it makes it easier to read and identify the scope of the fields quickly. As far as I am aware, there are no rules defining which style you should use.

Note: I develop in C# primarily.

stuartmclark
  • 449
  • 3
  • 9
  • there are conventions for java which say `camelCase` should be used for fields – ratchet freak Feb 04 '14 at 09:05
  • 2
    There are rules, like Stylecop for C# and the official Styling guides from MS (look for a list here: http://stackoverflow.com/questions/4678178/style-guide-for-c ) It is a matter of personal choice if you adhere to this rules. Perosnally, I stick to Stylecop because I think the rules are sensible. But moch more important is, that you are staying consistent with yourself, e. g. start private members with underscores every time, do not switch to small letters... – Christian Sauer Feb 04 '14 at 09:06
  • 1
    Does using camelCase indicate the use of prefixing the field name with an underscore? If I were to define that field it would be as private String _customerName; – stuartmclark Feb 04 '14 at 09:07
  • @ChristianSauer `The field-naming guidelines apply to static public and protected fields. Internal and private fields are not covered by guidelines` - http://msdn.microsoft.com/en-us/library/ms229012.aspx – Konrad Morawski Feb 04 '14 at 09:23
  • @KonradMorawski You are right. But Stylecop still screams at you if you are declaring a private Field starting with Uppercase. And for good reason: While internal naming is not so important for user of your app, it is quite important for you (and your team). – Christian Sauer Feb 04 '14 at 09:37
  • I don't know about yours, but my IDE has been showing member fields in a different color for at least 15 years. To quickly identify the scope of a variable, I simply look at the color – Bastian Voigt Mar 22 '21 at 06:54
0

In languages with language-level properties (haxe, actionscript, ..) an underscore is sometimes used when naming a private variable that is also accessible using a property. The point here is avoiding a name collision between the property and the private value.

private var _width:Number;

public function get width():Number
{
    return _width;
}
xastor
  • 101