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
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 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.