2007-07-23

Hungarian notation

Hungarian notation was, and is, a good idea.
Unfortunately prefixing the variable with the type has today become suffixing:
sCustomerNumber -> customerNumber
sPassword -> passwordString
nMax -> maxValue

What is the reason for this? Is it harder to read nCustomer than customerNumber? I don't think so. Instead more space/characters are used for communicating the same information.

Think of the ever present"customer number". Written as customerNumber at first look it looks like a number, a series of figures. Now we know that a customer number has dashes and letters in them so it is really a string, like customerNumberString. Written with hungarian notation it is sCustomerNumber and clearly a string.

Now I have come to use non camelCasing like everyone else but only for strongly typed languages like C# which can by itself differ between strings and integers. For loosely typed languages like Javascript or PHP i still use hungarian notation.

My rules for hungarian notation in Javascript is like:
sXXX for strings
nXXS for integers (bytes, long integers, whatever)
fXXX for floats (doubles, decimal, whatever)
oXXX for many objects (oUser, oBoat, whatever)
myclassXXX for other object (userProtagonist, accountMain, accMain, whatever)
rsXXX for record sets
bXXX for booleans
anXXX for arrays of integers
asXXX for arrays of strings
cXXX for constants and characters - I am not totally happy with this

There has been no reason to make it more complex than that.

2 comments:

Edward said...

I am afraid I have to disagree with you and it's all your fault since you lent me Fowlers book on refactoring. Hungarian notation where you prefix variables with a type denominator defeats refactoring. Or at least adds another hurdle, changing the datatype means you have to rename the variable as well.

I'm willing to add an m or underscore to private member variables in order to distinguish them from local variables as well as public variables.

Public: Value
Private: mValue (or _value)
Local: value

LosManos said...

The type denominator is in hungrarian notation in the front. Easy to recognize.

In the prefered notation of dotnet the same type denominator is placed in the end instead. Easy to mistake for a part of the word.

As long as one marks the type, prefix or suffix, the refactoring problem remains.

One would have a slight chance though with hungarian notation since it is easier to recognize. The automatic refactoring could give a warning that it looks-like-the-variable-name-should-be-altered.