Variable Prefixing in OO Languages.

Started by
30 comments, last by Nathan Baum 18 years, 4 months ago
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).
Advertisement
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
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
Quote:Original post by Thevenin
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).


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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Quote:Original post by kSquared
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


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

Hrm.. I might end up making a deritivate of my Fleurian Notation for C#.

This'll definently be interesting (If not difficult)...
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]
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;}


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
'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.
Quote:Original post by etothex
I 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.

This topic is closed to new replies.

Advertisement