views:

68

answers:

3

What is the general meaning and where did it start to have variables with leading or trailing underscores?

ex: _varName and varName_

I have always thought a leading underscore indicated the variable was a pointer but I don't know where I got this idea. Recently I have seen these naming conventions used in Java which makes that reasoning completely wrong.

+1  A: 

As far as I know, the initial underscore has been the recommended convention for case insensitive languages, eg. VB, denoting private members, usually found together with a public property without said underscore.

David Hedlund
+1  A: 

It depends a bit on the programming language.

In C/C++ the _varname microsoft used it for naming internal compiler variables to distinguish them from user variables. Lately though I have started to see both prefix _ and postfix _ as a way to denote instance variables in various languages.

Anders K.
A: 

Although this probably doesn't contribute to the origin story of either case...

In Perl, a leading underscore is a social contract for a poor man's private member. There are no private members in classic Perl OOP, so using an underscore is a means to communicate that this field or method should not be touched, modified, or invoked directly.

In practice, I usually use a trailing underscore to recreate a variable, like when I'm refactoring. The original variable will be occupied by an old, crappy interface and the new variable, using the exact same name, will be using the shiny, beautiful interface.

var dog = 'golden retriever';
var dog_ = BRD_GOLDEN_RETRIEVER;

process(dog);
Shelter->Process(dog_);

Obviously the side-by-sided-ness is only temporary. dog_'s existence is transient.

Mark Canlas