Wednesday, May 03, 2006
Good Hungarian
We are used to think of hungarian notation as being a bad thing. However, not many people know that what we know as hungarian notation is a perverted version of the original pure and good hungarian notation. Joel Spolsky talks about that too.
Bad-hungarian is when you prefix a variable name with type information
eg:
the 'f' simply means float.
You can see you have type compatibilty on both sides, but it doesn't say anything about semantic compatibility. fWidth could be used as a measure expressed in metres, whereas fSize is expressed in feet!
Good-hungarian is when you prefix a variable name with semantic information
mtrWidth = ftSize
OR
metric_width = feet_size
you can see immediately that there is a problem there.
Both variables are of the same type (Float), yet they have a different meaning. The prefix difference make the error stand out clearly.
This is very useful in general, but I would argue that in ruby - with the dynamism offered by duck typing - good hungarian is very useful and we already use, without even thinking about it.
Bad-hungarian is when you prefix a variable name with type information
eg:
fWidth = fSize
the 'f' simply means float.
You can see you have type compatibilty on both sides, but it doesn't say anything about semantic compatibility. fWidth could be used as a measure expressed in metres, whereas fSize is expressed in feet!
Good-hungarian is when you prefix a variable name with semantic information
mtrWidth = ftSize
OR
metric_width = feet_size
you can see immediately that there is a problem there.
Both variables are of the same type (Float), yet they have a different meaning. The prefix difference make the error stand out clearly.
This is very useful in general, but I would argue that in ruby - with the dynamism offered by duck typing - good hungarian is very useful and we already use, without even thinking about it.