The evolution of software practices ebbs and flows over time, and it seems that Hungarian notation is currently out of favour. This is especially apparent with some of the new developers that have joined the team here at Caplin. One of the first questions they ask is often about the naming convention that we use.
var declaration for a particular local variable within a function then the variable is added to the global scope, with the potential of nasty unforeseen side effects.
b– a Boolean variable. We only expect it to have a value
false. For example
e– a DOM element. Provided its not
nullwe expect to be able to access
childNodesand so on. For example
f– a function pointer. Typically this will be invoked at some stage in the future. For example
m– a map. For example
n– number, either a integer or a float. For example
o– an object. We don’t try to differentiate between all the different object types we have defined within our code with dozens of different prefixes, this would get too complicated to remember. Typically we use JsDoc to identify the types, if the scope of a variable is not small enough to see its type. For example
p– an array. This should really use an
aprefix, however this is a throw back to the original Hungarian notation from my C++ days where an array was just a pointer. For example
s– a string. Provided its not
nullwe expect to be able to invoke methods like
match(). For example
v– a variant, where various different types might be allowed. A typical example of where this might be used is a generic debugging method that is happy to take any type of argument and output it to a log. For example
m_– private member variable. Although there are techniques to hide member variables there are performance costs associated with them. The
m_prefix is a gentle reminder that only the class the member variable is defined in should be accessing it. In fact, when the code is obfuscated these private members may also be obfuscated since they aren’t part of of the public interface of the class. For example
g_– global variable. These are used sparingly. If a variable has a value to assigned to it within a method but does not have a g_ prefix then beware, this is probably a mistake and we now have an unexpected side effect. For example
- All other variables, such as local variables or function arguments, have no scope prefix.
Horses for Courses