Freigeben über


Pedantic Coder: Hungarian Notation

I generally dislike Hungarian Notation.  I particularly dislike what Wikipedia calls "System Hungarian", where the prefix indicates data type, as it adds almost no value for me.  I dislike the more semantically oriented form of Hungarian notation where the prefix implies some broader attribute (like "safety") because, though that does provide some value - I'm generally not fond of terse prefixes.  If something is an unsafe X then call it "unsafeX", not "uX" hoping that a reader will know u means unsafe.

I have taken to including units in variable names when they're (a) beyond what the underlying C/C++ types can discriminate between and (b) too basic to warrant the creation of a class.  For example, when a timeout is in milliseconds or microseconds i'll name the variable "timeoutMs" or "timeoutUs" so that I know which units the value is in.  I don't think that a "time" class is adding enough value here to create one and deal with publishing it, making it available for all the public interfaces I define, figuring out how to make it usable for C developers, etc...  But the difference between Ms and Us is quite large (both literally and figuratively).

When I do annotate variable names i tend to use suffixes - I find it's easier (for me) to parse a list of names if the real variable name comes first.  A block of variables named msX, msY, msZ, ... is simply harder to read. 

Despite this there's one set of terse suffixes that I have started using religiously - Cb, Cch & Ce.  These are count-of-bytes, count-of-characters and count-of-elements.  I have started using these everywhere in place of more general terms like "length" or "size" to indicate a counter for another variable.  I think about these as another form of unit, and it has saved my butt a couple of times – particularly if you start using the safe-string functions since you can easily see that your “length” matches the same units as the string function you’re calling.