-1

I once heard that a good convention for naming variables (in a strongly typed language) is by prefixing them with the first letter of the variable type.

For example (in C++):

int iInterestRate;
string sMessage;

I'm not sure what the advantages to doing this are. Are there any? And if so what are they? Is this a good and accepted convention for naming variables?

Chaim
  • 2,109
  • 4
  • 27
  • 48
  • 2
    For some background http://en.wikipedia.org/wiki/Hungarian_notation – Alex K. Jul 20 '11 at 13:46
  • This question should generate some comments. You are talking about _Hungarian notation_ and it is very controversial. It's been on SO quite a few times before. See for example http://stackoverflow.com/questions/111933/why-shouldnt-i-use-hungarian-notation. – Ray Toal Jul 20 '11 at 13:47
  • @Alex K. +1 I was beaten by 27 secs :p – Djole Jul 20 '11 at 13:48

1 Answers1

1

Sounds like you heard about Systems Hungarian Notation which used to be an accepted convention.

Was it good? Depends on who you ask. To me the prefixing was good, the reasons for it not so much.

These days Systems Hungarian is often greeted with great scorn -- because it was a mistake. The intent of the original (Apps) Hungarian was misunderstood, and what followed was a big mess.

Sure, there are some advantages to Systems Hungarian, Wikipedia attempts a list:

http://en.wikipedia.org/wiki/Hungarian_notation#Advantages

There is no shortage of heated commentary on the subject -- I won't regurgitate it all here -- I'll just submit that you shouldn't use a rigid system of prefixing variables with their data type.

However, if you want to know if there are advantages to prefixing variables with their logical type, the answer is an unqualified 'Yes'.

Look at Apps Hungarian:

http://msdn.microsoft.com/en-us/library/Aa260976

http://www.byteshift.de/msg/hungarian-notation-doug-klunder

I'm not advocating the whole thing as is, I'm just saying that their reasoning is sound.

The advantages and disadvantages of prefixing are highly subjective. Some say that it makes code hard to read, while others have no problem with it. Some find it harder to change names, others find it easier.

Me? I prefer my coding language to be easily distinguishable from human english. Prefixing gives me control over my code that I otherwise wouldn't have. I don't know or care if my code is hard to read -- I care that it's easy to understand.

So yes, a simple prefix convention is good. Just don't call it Hungarian.

dog44wgm
  • 535
  • 5
  • 17