Modified Hungarian Notation
May 2nd, 2009
Hungarian notation is a programming language naming convention. Writing with Hungarian notation entails creating variable names that start with a group of lower-case letters which are mnemonics for the type or purpose of that variable. Hungarian notation, fully implemented can be more trouble that it is worth. However, a subset of Hungarian notation can be useful.
A subset of Hungarian Notation
Modified Hungarian Notation (MHN) is a self-coined term, that described slight changes to normal Hungarian notation and also defines a subset of full Hungarian notation. The main point of MH is to help define scope and consistency of variable naming. We can differentiate between local variables and member variables that are semantically similar, by the prepended mnemonic; this results in less time wasted trying to determine a different variable name.
MHN Guidelines
Mnemonics
- integer i
- string str
- float f
- double dw
String Variables
Always prepend with the scope and the mnemonic:
string strMyName, g_strMyName;
Local Variables
Looping variables are: i, j, n, t
Short name local variables are the full type name but in lower case:
int counter(0), index(0), age; float average, rateofclimb;
If the type of the local variable is important (mathematical or type operations) then the type is specified in lowercase:
float fSomeValue;
Do not place “C” before the class name, instead use the class name itself in lowercase when instantiating:
MyClass myclass;
Global Variables
Prepend with g_ for example :
MyClass g_MyClass;
If the type is POD or string then always prepend the type mnemonic:
float g_fRateOfClimb; string g_strNameOfTheShip;
Class member variables
Preped with m_ and always for example:
class A
{
string m_strValue;
public:
A(string s) { m_strValue = s; }
};
A a("hello world");
Function Signatures
Single letter lowercase can be used for the variable name if the function is a setter. If the single letters conflict then append a number:
int MyFunction(string s1, string s2, int i, float f1, float f2);
If the function is non-trivial (not a settor or getter) then use the full semantic name in lowercase. If conflicting then append a number:
void MyFunction2(string strFirstName, string strLastName);
Leave a Reply