Hungarian Notation Again

JP has a nice post called ‘I promise’. I agree with his last two points. I will, however, still be explicitly setting my object variables to Nothing. He’s right that it’s unnecessary code, but I don’t think that makes it worthless. If setting objects to Nothing should be excluded, then so should comments. Comments don’t add anything to the code execution, they just convey the programmer’s intent. Setting objects to Nothing says ‘I’m done with this and don’t intend to use it anymore’.


But this post is about Hungarian Notation. The notation that I use is that I put a prefix in front of all of my variables that indicate scope and data type.

g – global
m – module
– procedure

Data Type
s – String
l – Long
d – Double
dt – Date
v – Variant
va – Variant Array
cls – Custom Object

For variables that refer to objects in a different type library, I make up some two or three letter abreviations. ol for Outlook, ac for Access, pp for Powerpoint and so on. For any other data types, I make something up on the spot. I don’t try to be consistent.

I think the scope prefix is useful and has helped me understand my own code when trying to decipher it later. I think the data type prefix is useful too, just like I did in 2005, but maybe not for the same reason. My main reason for sticking with data type prefixes is so I can have two variables with the same name.

Dim sFile as String
Dim lFile as Long

Open sFile for Output as lFile

I can’t say that data type prefixes have actually prevented coding errors for me, but the unique-variable-name advantage is undeniable. To me.

I want to use real Hungarian where the prefix denotes something more meaningful than data type, I really do. I’ve read exactly two examples of this, and I think they were both from Joel Spolsky. My problem is that I can’t come up with any examples of my own. You know, the kind of examples that I could use in real life. Maybe my level of coding is just too simplistic. I’ve never needed encoded strings in Excel VBA.

So it’s been four years (almost to the day) that we’ve discussed Hungarian. Maybe it’s time to discuss it again. Or maybe everything I’m currently working on in Excel is proprietary and I can’t post about it. Whatever the case, let’s hear your opinion. And if you have real world examples of true Hungarian, I’d love to hear them.

Posted in Uncategorized

16 thoughts on “Hungarian Notation Again

  1. Here’s a “new” idea modern EDI’s make variable notation less important, since establishing the type is very easy?


  2. This post made me realize how much my coding habits have changed since I began writing mostly .NET code! I used to write in Hungarian notation, but I have now totally abandoned it, even when I go back to VBA.
    I agree with Ross: modern IDEs makes checking the type of a variable so easy it’s not worthwhile putting that info in the variable name itself; hopefully at some point the VBA editor will be updated a bit, and include some of the cool features of Visual Studio…
    But even without that, I find that good variable names make the type obvious: for instance, I would tend to use Dim fileName as String.

  3. I’d agree with you about your first point, except that 99% of the code I see does this right before the End Sub, when objects would go out of scope anyway. Does anybody actually do this in the middle of a procedure? Also, what if you try to use the object later? Will the compiler notice?

    The problem seems to be that people *think* it’s doing something. So I don’t agree with your analogy about comments. I can’t deny that commenting code helps me when I’m trying to decipher my own code.

    Lately I’ve been using Shift-F2 to find the definition of a variable or procedure. But there are still times when Hungarian simply fits a particular situation. So it will be difficult to avoid doing so, especially when posting code samples. But I think its necessary.

    Re: your example, isn’t it confusing to use the same word in the variable name? lFile makes me think of a String, not a Long, despite the prefix.

  4. IMO, the sophistication of IDEs will never reach the point where prefix notation* becomes useless. Until an IDE can transmit the scope and data type of a variable directly to my brain with no action required, prefix notation will remain very useful. I want to obtain this information visually without having to do anything else. If a consistent naming convention of the variety that I’ve evangelized in PED has been used I don’t need to.

    * I don’t call what I use and recommend Hungarian Notation because it isn’t.

  5. WAY Back in the day when I wrote Fortran 2 on an IBM 360, i,j,k were used for counters (ostensibly i for iteration), n and m were for counts, and carrying over from physics, t for time, v for velocity, d for distance etc.

    Other variables were given representative names such as sal for salinity, Vo for “V sub o”, To for “T sub o” etc

    That grew to become names like RecordNum, NextRecord, IsFalse…

    28 years later, coding in C, it was the same. Hungarian came when I wasn’t looking. Growing up without it, I never quite did see the need for it, except when using ptrVAR to represent a pointer to variable VAR. That I always did, but I’ve never done it in VBA ;-)

    I really struggle to read code when the variable is just the next letter in the alphabet. Happens all the time when reading Euler code written by 14-yr-olds. ;-0


  6. I remain comfortable with using a prefix to indicate scope above the procedure. I even – long ago now – had the experience of imposed a prefixing convention on my then team but I wouldn’t do that now. I try to write in a way that renders it unnecessary, mostly through very small routines: if I have more then 8-10 lines of code or more than, say three or four local variables then I’m nervous. I still write larger procedures: it’s inevitable given the inefficiencies of VB as a language in these days, but I’m extra careful when I do.

    Prefixing user form objects is something I still do because I still find value in being reminded of the class of an object whose definition I can’t see while I’m coding. It works both ways too: I may not recall exactly how I named a control but typing its prefix lets Intellisense help me out.

    Mostly though, now I think about it, I’m good with any convention that a developer adopts if it’s apparent that they’ve considered its purpose and determined that it offers them a benefit. The problem comes with slavish adherence to, and inappropriate application of ill-understood practices handed down from generation to generation.

  7. Hey Dick, we’ve already had this discussion at LinkedIn, and I repeat what Linus Torvalds says … Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged, the compiler knows the types anyway and can check those, and it only confuses the programmer.

    Remember Shift-F2!

    Even your unique variable name is totally spurious, to me. Neither is a file, so sFile, lFile is bad naming, it should be Filename and FileIndex, or whatever they really are.

    The imporatnt thing is a naming convention, and stick to it, a decent coder will soon get into the swing.

    And you are right, comments are pointless (except the copyright notice ). As the great Smurf says, if you need comments to explain the code, it is badly written. Whenever I add comments because I think something is tricky, I invariably don’t understand it myself in 3 months. If I read someone else’s comments, I either cringe at the banality of it, e.g.

    ‘Close Recordset

    or I don’t understand what they are saying. And who ever updates comments? The code never lies, comments often do.

    And why have you got a picture of 2 Greek ladies on a topic about Hungarian notation?

  8. I agree with Bob (on the code side) dunno about the national dress, but I bet he’s right on that too.
    I have rarely read a useful comment in Excel VBA code, apart from the essential module header:
    ‘They made me do it this way, I wanted to do it right but they made me do it quick instead.

  9. Comments are occasionally necessary in numeric programming, in particular handling truncation error can vary between languages and/or compilers. The code needed to trap and remedy such pathologies is usually not straightforward.

    I’d also make the argument that trapping exceptions due to external processes benefits from comments explaining the nature and causes of such exceptions and the reasons why the exception handler does what it does. E.g., handling results from SQL queries that return no records. Whether that’s an ‘impossible’ or possible but (presumably) rare, some explanation of why it’s handled as it’s handled is a good thing.

  10. I find real Hungarian notation very useful as a link between datatype and semantics (obviously, stating the datatype as part of the name is useless).

    One Excel-relevant example I use every day is column and row numbers. They are both integers. I use the prefixes ‘col’ and ‘row’, for example rowSomething and colSomething. I use the same prefix in functions.

    In my systems I have the Excel interface located in a few classes that use Excel’s 1-indexed col- and row-numbers. In the rest of my code, I use 0-indexed col- and row-numbers. In this case, Hungarian prefixes are even more useful: ‘xr’ and ‘xc’ are 1-indexed, ‘row’ and ‘col’ are 0-indexed. Clearly, these shouldn’t be mixed, and the prefixes help spot problems.

    Another example is the C# DateTime structure, which can be used for either a date-only value or at date-and-time value. I use ‘date’ as prefix when the value is a date-only and ‘dti’ when the value is date-and-time. You can calculate a timespan by subtracting two ‘date’ values or by subtracting two ‘dti’ values, but you shouldn’t calculate a timespan from a ‘date’ and a ‘dti’.

  11. My coding techniques have certainly evolved as have others, such that I do not use a type-based notation all the time since in many instances that variable name makes it obvious.

    But what I never understood, and never followed, was the concept of using prefixes instead of suffixes (eg. bFound vs found_b). Why is the type more important than the variable name itself – if you use suffixes, then starting the variable name allows intellisence to find the variable name with fewer keystrokes – and reminds you once you see the full name, what the type is – which is all I need to ensure I am using the right type. I guess for me, the efficiency of less keystrokes was a deciding factor, but I also find (and this it appears is very personal) it hard to read variable names when there are a few characters in front that have little to do with the english understanding of the variable name. Even though they are lower case, it still gets in the way of me quickly understanding what the variable is meant to be. Plus suffixes allow me to use lower case variables for local and uppercase first letter in each word, to be module or global variables – certainly redundant with the suffix, but it still adds clarity to me.

    Does anyone know of a prefix/suffix interchange addin – when one has to take over someone esle’s code.


  12. About setting variables to Nothing, there are not so useless.
    You have to set them to Nothing when you deal with copy/paste Macros code in Macros, especially for VBComponent objects, otherwise, the execution crashes Excel when you run your macro twice.

    Excel is not good at memory management, thus setting Object to Nothing helps it a lot when using extensively macros !

    – Cyril.

  13. BP: Don’t tell me you can tell the difference between Greek and Hungarian women.

    I wish I could have come up with a better example than lFile. I guess I don’t run into that ‘problem’ enough, because if I did I would have good examples aplenty. It’s one of those ‘problems’ that I see rarely, and I’m glad I have a ‘solution’ for, but the cost of all that prefixing isn’t really justified. Oh, I know the example I was trying to think of: keywords as variables. Try naming a variable ‘Replace’. I know, bad examples everywhere. :)

    Does intellisense autocomplete variable names as you type them? If so, mine is broke and I need to get it fixed, because that would be great.

    The prefixes vs suffixes don’t bother me. First, no non-alpha characters in my variable names. Characters like shift+underscore are too prone to typos for me. I read the prefixes with the variable name, so it’s not particularly cumbersome for me, e.g. ‘bee found’ or ‘ell file’.

  14. Intellisense will not autocomplete variable names (In actuality, it doesn’t autocomplete anything, it just shows you what is available with your so-far typed characters), and ctrl-spacebar does that for variables.

  15. I read somewhere that If ElseIf ElseIf etc was a speed improvement on Select Case.
    For me, this was the final push I needed to avoid Select Case whereever possible.

Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.