_Underscores in Function Names
Solution 1
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.
Solution 2
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".
Solution 3
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
Related videos on Youtube
Alan Storm
Portland based Web Developer/Programmer/Engineer. Projects include No Frills Magento Layout, the only Magento layout book you'll ever need and Commerce Bug, the debugging extension for the Magento Ecommerce system. If you're interested in low cost, in-depth mentoring/tutoring, checkout my Patreon campaign.
Updated on July 09, 2022Comments
-
Alan Storm almost 2 years
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
- Where the technique first came from
- 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.
-
Christian Davén about 15 yearsIn Python it's not only a naming convention, but two leading underscores actually makes the members private!
-
Steve Jessop about 15 yearsActually, 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 stackoverflow.com/questions/228783/… or 17.4.3 in the standard.
-
Bill K about 15 yearsMember 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.
-
Steve Jessop about 15 yearsRightly 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...
-
Alan Storm about 15 yearsThat looks like the oldest, so a win for you. @onebyone, I'm starting to really understand why c++ gets such a bad rap now :)
-
Joakim almost 9 yearsI assume the link to the underscore wiki is meant to refer to the programming one (disambiguation was added after this link was added to the answer I think). So should probably changed to: en.wikipedia.org/wiki/Naming_convention_(programming) ?