Poll: Hungarian notation?

Status
Not open for further replies.

meldroc

Ars Tribunus Angusticlavius
9,494
Ugh! Hungarian notation is the work of Gates himself!

I'd say the best way to break him of his fondness for Hungarian notation is to take it to an extreme. Make a pointer to a handle to a member of a class (using Win32 hideousness), and make your variable names so indecipherable it drives him mad. When he complains, just tell him you're using Hungarian notation, what's the problem? It's supposed to make the code easier to read. :p
 

w.anders

Ars Tribunus Angusticlavius
6,957
You need to start using that stick, boy. Pound the stupid out of him.

Failing that, drive him nuts thusly:

Hungarian Notation: Hungarian Notation is the tactical nuclear weapon of source code obfuscation techniques; use it! Due to the sheer volume of source code contaminated by this idiom nothing can kill a maintenance engineer faster than a well planned Hungarian Notation attack. The following tips will help you corrupt the original intent of Hungarian Notation:

  • Insist on using "c" for const in C++ and other languages that directly enforce the const-ness of a variable.
  • Seek out and use Hungarian warts that have meaning in languages other than your current language. For example insist on the PowerBuilder "l_" and "a_ " {local and argument} scoping prefixes and always use the VB-esque style of having a Hungarian wart for every control type when coding to C++. Try to stay ignorant of the fact that megs of plainly visible MFC source code does not use Hungarian warts for control types.
  • Always violate the Hungarian principle that the most commonly used variables should carry the least extra information around with them. Achieve this end through the techniques outlined above and by insisting that each class type have a custom wart prefix. Never allow anyone to remind you that no wart tells you that something is a class. The importance of this rule cannot be overstated: if you fail to adhere to its principles the source code may become flooded with shorter variable names that have a higher vowel/consonant ratio. In the worst case scenario this can lead to a full collapse of obfuscation and the spontaneous reappearance of English Notation in code!
  • Flagrantly violate the Hungarian-esque concept that function parameters and other high visibility symbols must be given meaningful names, but that Hungarian type warts all by themselves make excellent temporary variable names.
  • Insist on carrying outright orthogonal information in your Hungarian warts. Consider this real world example: "a_crszkvc30LastNameCol". It took a team of maintenance engineers nearly 3 days to figure out that this whopper variable name described a const, reference, function argument that was holding information from a database column of type Varchar[30] named "LastName" which was part of the table's primary key. When properly combined with the principle that "all variables should be public" this technique has the power to render thousands of lines of source code obsolete instantly!
  • Use to your advantage the principle that the human brain can only hold 7 pieces of information concurrently. For example code written to the above standard has the following properties:
    • a single assignment statement carries 14 pieces of type and name information.
    • a single function call that passes three parameters and assigns a result carries 29 pieces of type and name information.
    • Seek to improve this excellent, but far too concise, standard. Impress management and coworkers by recommending a 5 letter day of the week prefix to help isolate code written on 'Monam' and 'FriPM'.
    • It is easy to overwhelm the short term memory with even a moderately complex nesting structure, especially when the maintenance programmer can't see the start and end of each block on screen simultaneously.

-.-

I think if your code is poor, then Hungarian notation might be useful.

Solution: Don't write poor code. Declare and initialize variables right before their use.
 

dhopton

Ars Tribunus Militum
2,009
Hungarian notation has it's places. Somtimes it's good, sometimes it's bad. It depends exactly what you are doing. When working with languages such as VB, Java, C# where the language is heavily typed, then it really does make things look shite,and kill readability in the code.

In well thought out OO C++, it also looks f-ugly.

However, I find in nice, straight run of the mill C procedural stylee, it can be helpful, espcially when working with Win32.

Generally, I avoid variables more than 15 chars long because it starts to get screwy (in readability stakes). :)
 

Malyk

Ars Tribunus Angusticlavius
8,558
Subscriptor
I like using hungarian (or modified hungarian) naming for my controls in Visual Basic. Other than that tough I just name my variables whatever I want to.

Example: txtName, or cboYear and result, value, etc.

Naming the controls gives me an immediate knowledge of what I can do with them. I know txt* is a textbox while I might not remember that * is a textbox vs. a label, and vice versa. Variable naming is different though because there use is usually short lived and I can see the declaration easily.

Mike
 
I like using hungarian (or modified hungarian) naming for my controls in Visual Basic. Other than that tough I just name my variables whatever I want to.

Example: txtName, or cboYear and result, value, etc.

Naming the controls gives me an immediate knowledge of what I can do with them.
I have nothing against the name saying what kind of control it is, but not the way Hungarian notation wants to do it.

It makes the code pretty much unreadable.
 

AlphaMeridian

Ars Legatus Legionis
17,136
Moderator
Is this coworker your superior? If not, then you need to lay down that you're NOT going to made your code totally unreadable. If he is, then i'm sorry, unless you're willing to quit over the issue.

Personally, I name my variables whatever I want and then just comment them to tell the user EXACTLY what they are, in english.

-Alpha
 

Evil Peer

Ars Scholae Palatinae
942
Well, I'm personally against HN, but I do use a very small set of it:

s - String
c - Char (something that is specifically used as char and not string)
f - Float
b - Bool
n - Number (int, unsigned char, etc)

I think that's it.

Certainly much better than the pfncb that I've seen some of predecessors use (pointer to a callback function).

I also do name some variables the same, though only where it would make sense. Such as sPID would be the string read in, and nPID would be the string converted to numeric. In such a situation, to accurately indicate what each of the variables would be for, you would still need to indicate in the variable name somehow what the type was (ProcessStr and ProcessNum maybe). Can't get much shorter than one char though.

---Evil Peer
 

MrMills

Ars Praefectus
3,727
Moderator
I find I use a form of mini-Hungarian with untyped languages like VBScript or JavaScript (iInteger, sString, fFloat, bBoolean, oObject, dDictionary), but when using a language with an IDE to match, I usually only prefix for scope (FObjectField, GGlobalVariable) and otherwise don't at all.

VB-style prefixes for controls work well, though, because I can group controls together mentally (e.g. lblPassword and txtPassword).
 

wb

Ars Legatus Legionis
25,208
Subscriptor
Naming the controls gives me an immediate knowledge of what I can do with them. I know txt* is a textbox while I might not remember that * is a textbox vs. a label, and vice versa. Variable naming is different though because there use is usually short lived and I can see the declaration easily.

Why not just put "check" or "text" in the variable name? It's more descriptive and easier to read. This is typically what I do to identify controls in VB. e.g. LastNameEdit, DescriptionText, BrowseButton, etc.
 

wb

Ars Legatus Legionis
25,208
Subscriptor
That would probably work just as well. txtName is essentially the same as textName, or NameText and easier to write. Either way works, I was just taught the prefix way.

Abbreviated prefixes like cboRememberPassword, trProject, etc. are cryptic IMO, especially for newer developers, whereas "ProjectTree" is self-explanatory even to someone new to VB or your code. I don't find typing the name out any harder than typing the prefix, but maybe that's just me.
 
Prefixes are sorted by control/data type in an alphabetical list. This can be helpful when using an IDE with code completion.

This is also a minus for postfixed logically linked routines. ReadThing, WriteThing, DeleteThing do not show up together in an alphebitized list (code completion, documetation, online help). ThingRead, ThingWrite, ThingDelete do show up grouped together nicely but are less english-like because the verb is after the noun. I prefer NounVerb in these cases.
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Thanks for the input, guys. (No, I don't think I'm going to resort to violence.)

w.anders, I'm quite familiar with How to Write Unmaintainable Code. ;) Part of me is tempted to sabotage my code intentionally by using the wrong prefixes...

"What? No, piFoo is not really a pointer to an int. It's actually a pointer to a pointer to an int. I had to change the type but forgot to change the variable name. Oh, and be careful about using a global-search-and-replace, because I've got a bunch of other variables named piFoo elsewhere that really are pointers to ints..."

(As much as I'd like to do this, though, the risk of it backfiring and confusing myself are too great.)
 

Aeirould

Ars Tribunus Angusticlavius
7,535
Subscriptor
I do prepend variable types only in one situation: When I am dealing directly with a database.

I used to prepend a lot more types, but the first time I actually made a distributable class other people would use and saw that I was creating a situation where someone had to do:

foo.strName = "Blah"

I revolted, because that's just revolting.

The reason I still use it on database-related variables (and *only* there) is that it is: a) a reminder of the database variable type, which may not precisely match the variable type in the language I'm in, such as the bit SQL data type vs the Boolean language type, and b) it immediately indicates that the variable I'm working with *is* a database variable. Thus, strLastName will be used internally within the object that talks to the database, while the exposed (and filtered) property would be LastName.

This also helps in reminding maintainters, including myself, that changing that variable type (say a value no longer will fit in an int) requires a change to the database as well. There may well be a better way to do this, *but it works for me and is not an obstruction to maintance*.

The bitch is when I have to go into old code when I was still msBrainWashed... do I leave the shit in place or take the time to strip it?
 
Status
Not open for further replies.