   Using underscore in class and variable names
  I've seen in a lot of examples online, on this site and in books that precede class names and instance names with a single or double underscore, something like '__object'.What's the reason for this? And is there any significance to the use of a double one instead of a single one?   
  I think it's used to indicate that a variable is an implementation detail and is not to be seen/used by client code. I've never used this convention myself, though. I think it's pretty ugly, and I don't really see the need.   ;Quote:Original post by AristeI think it's used to indicate that a variable is an implementation detail and is not to be seen/used by client code. I've never used this convention myself, though. I think it's pretty ugly, and I don't really see the need.I'm pretty sure that's how it was used in AS2, but they stopped using them in AS3.I think the most important thing is that you stick to whatever style the rest of your code is in. If you work for a company they probably have a set of standards to follow somewheres. If you're doing it yourself, the most important thing is that you're consistent.   ;  It's a naming convention in Python that a single underscore preceding a name indicates that it is "internal" -- basically a conventional way of saying "private", since Python doesn't actually have an enforced privacy policy on member fields. Likewise, a double underscore is used for language internals -- fields and functions that the language creates for you, and which you shouldn't be changing (or, generally, referring to).   ;Quote:Original post by way2lazy2careQuote:Original post by AristeI think it's used to indicate that a variable is an implementation detail and is not to be seen/used by client code. I've never used this convention myself, though. I think it's pretty ugly, and I don't really see the need.I'm pretty sure that's how it was used in AS2, but they stopped using them in AS3.I think the most important thing is that you stick to whatever style the rest of your code is in. If you work for a company they probably have a set of standards to follow somewheres. If you're doing it yourself, the most important thing is that you're consistent.I prefix my variables (in AS3) with an underscore if they are private and have accessor methods. So the private _center variable's accessor would be center. The advantage of this method is I can swap a "public" variable for a "private with accessors" or vice versa and not have to refactor anything outside the class.;  in C++, i believe the compiler reserves all symbols that are preceded by _ or __(ie you shouldn't name your symbols that way, but if you do it will PROBABLY be fine and the internet police aren't going to come after you)(:   ;Quote:Original post by justkevinI prefix my variables (in AS3) with an underscore if they are private and have accessor methods. So the private _center variable's accessor would be center. The advantage of this method is I can swap a "public" variable for a "private with accessors" or vice versa and not have to refactor anything outside the class.I think it's mostly because a bunch of the keywords in AS2 that had underscores were switched to no underscore.   ;Quote:Original post by BattleMetalChrisI've seen in a lot of examples online, on this site and in books that precede class names and instance names with a single or double underscore, something like '__object'.What's the reason for this? And is there any significance to the use of a double one instead of a single one?It depends on the language.The general convention is that it denotes a private element that you should not use.In the C++ standard we find:Quote:Certain sets of names and function signatures are always reserved to the implementation:— Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.There are people who argue that it is safe to use both double underscores and initial underscores within namespaces or within classes.  There are also people who forget that even CONTAINING a double underscore is reserved, not just an initial double underscore. Because initial underscores and double underscores are also used in macros, and macros don't respect scope, the general rule in C++ is to avoid both of them completely.In practice the current implementation details about underscores are common and well known, so many people will use them even when they shouldn't.Other languages obviously have different rules and requirements.   ;Quote:Original post by Atrix256in C++, i believe the compiler reserves all symbols that are preceded by _ or __I believe the rules are a little more involved then that. I'm pretty sure that the symbols reserved for the implementation are as follows:1. Symbols in the global namespace beginning with an underscore.2. Symbols beginning with an underscore followed by a capital letter.3. Symbols containing two consecutive underscores.   