# Variable Prefixing in OO Languages.

## Recommended Posts

Thevenin    270
In C it was easy. [Variable] g - Global l - Local i - Integer c - Character s - Short o - Instance of a Struct t - Struct u - Unsigned [Function] s - Procedure f - Function eg...
int giJoeHealth;
unsigned int guiJoeRating;

void main(int liTesting)
{
giJoeHealth = liTesting;
}


In an object orianted language, there must be a hundred different prefixes! In specific to C#, how would you guys go about prefixing it? Or would you not even bother (I'm kinda borderline with doing so or not).

##### Share on other sites
kSquared    1356
An entire section of the MSDN C# reference is devoted exclusively to naming guidelines for various classes and class members. See here.

Hope that helps,
-- k2

##### Share on other sites
Endar    668
Quote:
 Original post by TheveninIn specific to C#, how would you guys go about prefixing it? Or would you not even bother (I'm kinda borderline with doing so or not).

I'm actually using C++, but anyway, I really don't need to do it to that extent.

I mean, why would you bother with putting prefixes on local variables? If the function is that large that it's hard to keep track of them all, you probably need to split it up into more than 1 function.

With my class member variables, I prefix them with 'm_' to indicate a class member, but nothing past that. I haven't needed it, and again, if your class consists of more than 15 (probably a little large at that) data members, then you should at least consider splitting it up into more than 1 class.

##### Share on other sites
Thevenin    270
Quote:
 Original post by kSquaredAn entire section of the MSDN C# reference is devoted exclusively to naming guidelines for various classes and class members. See here.Hope that helps,-- k2

Helps a little, but its very indirect (Too indirect for me to understand it).

##### Share on other sites
Thevenin    270
Hrm.. I might end up making a deritivate of my Fleurian Notation for C#.

This'll definently be interesting (If not difficult)...

##### Share on other sites
Flimflam    665
The only time I ever really feel the need (desire really) to prefix a variable is in the declaration of a function.

int GetSomeNumberFromOtherNumber(int nNumber1, int &rnNumber2, int *pnNumber);

Other than that, I really don't prefix anything other than classes private members with m_Whatever.

Of course, I am referring to c++.

[Edited by - Flimflam on November 28, 2005 2:47:07 AM]

##### Share on other sites
hh10k    589
I don't really see the point in hungarian notion. Any decent IDE nowadays will tell you on the spot what type a variable is, and it just causes headaches when you're trying to use other people's code. I much prefer to name variables by what they contain, or typedef things that have a particular meaning. The only exceptions I have are m_ for member variables (because initializer lists in C++ can be difficult when there are parameters with the same name), and g_ for global variables.

typedef int PlayerHealth;typedef unsigned int PlayerRating;PlayerHealth g_joeHealth = 0;PlayerRating g_joeRating = 0;// I have no idea why main is just taking a single int -- no matter :)void main(PlayerHealth testing){     g_joeHealth = testing;}

##### Share on other sites
CJM    454
Hey,

My preference is to not have any prefixes. So long as the IDE that you're using can describe types it's not particularly useful anyways IMO.

If code is particularly complex, then occasionally I describe the value expected in the name, but this is usually only in the prototypes [rotationD, rotationR, rotationDCW and the like are more important to me than putting in some information that can be picked up by intellisense or examining the header/documentation manually].

I also use the syntax where I can't overload functions, but I postfix with the type [so that if you're searching for function func, but don't know the specific type, you can look alphabetically rather than looking by type].

Just my thoughts, it's rare for the prefixes/postfixes on variables and functions to be useful in my experience. It may be more explicit, but it's also at least 1 more [redundant] keystroke per variable.

--CJM

##### Share on other sites
etothex    728
'Nuff said

I don't prefix variables, except sometimes putting _ in front of class members. What point is there? If the (local) variable declaration is too far away from the usage, then your methods are too big.

##### Share on other sites
Oluseyi    2103
Quote:
 Original post by etothexI don't prefix variables, except sometimes putting _ in front of class members.

Which, of course, is illegal in Standard C++ (identifiers with leading underscores are reserved for the standard library implementation).

Use what you want, just be consistent, and accept that virtually nobody else will agree with you.

##### Share on other sites
Thevenin    270
Quote:
 Original post by OluseyiUse what you want, just be consistent, and accept that virtually nobody else will agree with you.

Thats going in my little collection of great phrases. [smile]

##### Share on other sites
DaTroof    162
I seem to be in the same boat with most of the other replies. I consistently use prefixes in two places: g_ for global variables and _ for class members (but only private or protected, not public). I also use camel humping for the rest of the name, and never use underscores anywhere else except enums and preprocessor defines, which are always capitalized. One reason for my system, it's easy to find globals that I probably intend to encapsulate elsewhere.

Another argument against verbose notation: if you change the variable's data type, you need to change the name (intName to lngName or whatever). So much for encapsulation and reuse. That's also why I limit the underscore prefix to private/protected class members. They're the only variables whose context I always need to differentiate.

Quick example of how I do it:

#define MAX_ARRAY_SIZE 1024class aClassOfMine {    private:        int _classMember;    public:        int classFunction();};aClassOfMine g_myClassArray[MAX_ARRAY_SIZE];

##### Share on other sites
Thevenin    270
Quote:
 Original post by DaTroofAnother argument against verbose notation: if you change the variable's data type, you need to change the name (intName to lngName or whatever).

Yah, thats been my only problem with the prefix system. =/

##### Share on other sites
etothex    728
Quote:
Original post by Oluseyi
Quote:
 Original post by etothexI don't prefix variables, except sometimes putting _ in front of class members.

Which, of course, is illegal in Standard C++ (identifiers with leading underscores are reserved for the standard library implementation).

No, AFAIK, identifiers are only prohibited with leading double-underscores, or global identifiers with leading underscores. [draft 17.4.3.1.2]

Unless the final standard is different, or I am interpreting the standard incorrectly (in that case, one leading underscore could _only_ be used by the implementation in the global namespace, which doesn't make sense), I am ok using _ in private class identifiers.

I usually use java for OO, and code procedurally in c++, which if I'm wrong shows why I have not reached the conflict earlier :)

EDIT: I correct myself, one leading underscore followed by an uppercase letter is reserved an any namespace. But underscore + lowercase should be ok.

Quote:
 Use what you want, just be consistent, and accept that virtually nobody else will agree with you.

Yep, just be kind to those reading your code and use descriptive identifiers (and comment).

##### Share on other sites
Nitage    1107
Quote:
Original post by Oluseyi
Quote:
 Original post by etothexI don't prefix variables, except sometimes putting _ in front of class members.

Which, of course, is illegal in Standard C++ (identifiers with leading underscores are reserved for the standard library implementation).

Use what you want, just be consistent, and accept that virtually nobody else will agree with you.

It is not illegal.

The following identifiers are reserved:
Quote:
 ISO 14882, section 17.4.3.1.3:- Each name that contains a double underscore or begin with an underscore followed by an upper-case letter.- Each name that begins with an underscore is reserved to the implementation in the global namespace

##### Share on other sites
Guest Anonymous Poster
just don't go overboard with all the pre/suffixing and naming conventions.. intellisense is our friend.

##### Share on other sites
Nitage    1107
Indeed. In a language where you can define custom types, this kind of prefixing becomes totally redundant.

##### Share on other sites
Oluseyi    2103
Prefixes should indicate use, not type. Ironically, this was Simonyi's original intention with what is popularly known as "Hungarian Notation" (or derisively known as Microsoft Notation). Unfortunately, being Hungarian, his facility with English was limited and he used the word "type" where he meant "kind," and we have suffered ever since under the yoke of Petzold.

##### Share on other sites
Nitage    1107
Quote:
 Original post by OluseyiPrefixes should indicate use, not type. Ironically, this was Simonyi's original intention with what is popularly known as "Hungarian Notation" (or derisively known as Microsoft Notation). Unfortunately, being Hungarian, his facility with English was limited and he used the word "type" where he meant "kind," and we have suffered ever since under the yoke of Petzold.

Use == Type in OO.

Take the often quoted safe-string / unsafe-string article. It's about javascript - it's totally irrelevent to C++ as in C++ you'd simply have two classes with (explicit) conversion so that using an unsafe-string in place of a safe string becomes a compiler error rather than a bug which can be spotted by examing the variable names.

##### Share on other sites
Enigma    1410
How has nobody posted a link to the wartHogs article yet?

Enigma

##### Share on other sites
kSquared    1356
Quote:
Original post by Nitage
Quote:
 Original post by OluseyiPrefixes should indicate use, not type. Ironically, this was Simonyi's original intention with what is popularly known as "Hungarian Notation" (or derisively known as Microsoft Notation). Unfortunately, being Hungarian, his facility with English was limited and he used the word "type" where he meant "kind," and we have suffered ever since under the yoke of Petzold.

Use == Type in OO.

I'm not sure I follow you here. But if you're saying that the manner in which an object is used uniquely defines its type, I have to disagree strenuously. The obvious example is any single-inheritance chain: common base types can substitute for the specialized types (aka LSP).

##### Share on other sites
Nitage    1107
Quote:
Original post by kSquared
Quote:
Original post by Nitage
Quote:
 Original post by OluseyiPrefixes should indicate use, not type. Ironically, this was Simonyi's original intention with what is popularly known as "Hungarian Notation" (or derisively known as Microsoft Notation). Unfortunately, being Hungarian, his facility with English was limited and he used the word "type" where he meant "kind," and we have suffered ever since under the yoke of Petzold.

Use == Type in OO.

I'm not sure I follow you here. But if you're saying that the manner in which an object is used uniquely defines its type, I have to disagree strenuously. The obvious example is any single-inheritance chain: common base types can substitute for the specialized types (aka LSP).

The use of the class from the perspective of the user equates to the type which the object is referenced with (the base class).
I didn't say anything about uniqueness.

##### Share on other sites
me22    212
I agree that prefixes should be dropped when it's possible to just use the type system instead, since the compiler's much more relyable at checking things than humans.

And anyways, how the hell are you supposed to prefix for templated types? >;]

Oh, and regarding the first post:
unsigned int guiJoeRating; // <-- sure looks like a badly-named GUI widget to me

##### Share on other sites
godecho    235
http://www.joelonsoftware.com/articles/Wrong.html

interesting article on variable prefixes, and some history/true story type stuff about hungarian notation. excellent read.

edit: this is the article Oluseyi and Nitage are talking about.

##### Share on other sites
Zahlman    1682
Two ironic things.

1) The time I most want to actually use any kind of Hungarian notation is when I'm working in Python. :s

2) The people who seem to defend HN the most strongly are the ones who are also fond of fancy IDEs which are supposed to provide the exact same information. (But then, looking at their code, it seems that redundancy is indeed no problem for these people... sigh...)

## Create an account

Register a new account