Variable Type Prefix Convention
I once heard that a good convention f开发者_运维问答or 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?
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.
精彩评论