views:

598

answers:

5

In a lot of languages with simple OO capability (PHP 4), or misunderstood OO capabilities (Javascript, C using function pointers, etc.), you'll end up with a function naming convention that uses leading underscores to to indicate privilege level.

//ex.
function _myPrivateFunction(){
}   

While individual teams are always going to come up with their own naming conventions like this, the underscore convention seems so prevalent that it made me curious about

  1. Where the technique first came from
  2. If there was ever any standardized systems (sort of like hungarian notation) developed around the convention

Beyond pure curiosity, I'm seeing this in a few codebases I'm dealing with right now, and I'd like to understand the possible headspaces of the developers who originally came up with it.

A: 

I first saw it when coding C++. Marking member variables with "m_" prefix was commonly done.

I prefer to not use any of that when I write Java. I make member variables clear using "this".

duffymo
Member variables are automatically marked in java because the tools use static analysis. Look at the color of your variables in whatever editor you use! Putting this. in front implies you are relying on it for recognition. If you are relying on some human remembering to use .this instead of a machine that KNOWS exactly what the variable is, you're obviously relying on the wrong indicator.
Bill K
That's true, Bill. I stand corrected.
duffymo
Rightly or wrongly, I suspect some people still write (or especially read) Java using text editors which can't do decent (or any) code analysis. I try to treat syntax-highlighting as something that helps me edit, not as something other people need if they want to be able to read my code...
Steve Jessop
+3  A: 

In C++ world, member names that start with underscore are reserved for use by compiler (or low level STL like API) developers. It's not prohibited by compilers in any way, but that's the tradition.

This wiki link is quite informative on underscore.

Koray Balci
Actually, all names starting with an underscore are reserved in the global namespace, but only names containing double underscore or starting with underscore and a *capital* letter are reserved in all contexts. See http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier or 17.4.3 in the standard.
Steve Jessop
That looks like the oldest, so a win for you. @onebyone, I'm starting to really understand why c++ gets such a bad rap now :)
Alan Storm
+2  A: 

I cannot tell you the origin of this convention. My guess is, since the underscore is the only non-alphanumeric character allowed in identifiers in most programming languages, it is natural to chose it as a prefix for private members.

In Python, prefixing names with an underscore is more than just a convention: Symbols starting with an underscore are not imported by default when importing "everything" from a module, therefore the underscore indicates "private" / "internal usage".

Ferdinand Beyer
A: 

underscore ( _ ) stands for a private / protected function or variable.

Don't know WHO actually came up with it, but I know it is "supported" by Zend ( and the Zend coding standards ).

edit : http://framework.zend.com/manual/en/coding-standard.naming-conventions.html

-> section B.3.4 -> paragraph 2

mhd
A: 

I've seen underscore used as private functions and I've seen seen underscore as global functions. Also underscore is used to denote global variables within PHP itself.

$_POST $_GET $_SESSION etc..

It's just a naming convention so I'd ask the author, if he's around.

Ólafur Waage