Hungarian in VBA okay?
Asked Answered
T

4

6

I don't use hungarian (str, int) prefixes in .Net, but I still find it useful in VBA, where it is more difficult to see types.

Is this bad? Unnecessary? Maybe I'm missing something.

I'd really appreciate any feedback. I've been wondering for a while.

Thanks everybody.

Twospot answered 3/4, 2010 at 22:53 Comment(3)
I was hoping for more answers directly about using it in VBA. I see answers all over StackOverflow from people with high reputation scores (like Joe at the link below) who use hungarian in their VBA answers. There has to be a reason for that. https://mcmap.net/q/383187/-when-to-use-a-class-in-vbaTwospot
I think you see it a lot because it was part of the culture of Windows programmers in the 1990s, and that was when (pre-.NET of course) VB was very popular, and so it naturally became part of the culture of VB and thus VBA programmers. There is no feature of VBA that makes "Systems Hungarian" (see the link Earlz posted below: joelonsoftware.com/articles/Wrong.html) more "O.K." in that language. It's definitely "unnecessary", and that technically makes it "bad", but in the sense that if good code gets a 10, good code with Systems Hungarian gets a 9.99.Ichthyosis
I do think that VBA lends itself to "Apps Hungarian" more than other currently popular languages, though, especially in conjunction with Excel. Lots of use of the 'Variant' data type, plus the inconvenience of defining classes, means that putting semantic information in your variable names makes correspondingly more sense.Ichthyosis
H
5

I always use one and two letter prefixes in VBA. I'm sure I'm the only one that's going to admit that, but I figured somebody needed to be the contrarian.

Of the 18 million lines of VBA code I've written, I've collaborated on about 1,000. If nobody else sees my code, then I'm free to use a convention that I like. If someone else will be working on your code, you should agree on a convention.

I like that it lets me keep my variable names shorter. I could use FileNumber and FileName or I could use lFile and sFile. I don't find one more or less readable than the other. It also helps me use reserved words as variables. If I want to use Replace as a variable name, I can't. But I can use sReplace or lReplace.

Hypochondriac answered 4/4, 2010 at 0:39 Comment(2)
Thanks for the response. The thing about the responses is that I've never seen anyone use hungarian in .Net code, but also, I've NEVER seen anyone NOT use hungarian in VBA. So I'm surprised that you are the only one that responded to say you use it in VBA. I appreciate all the answers, but I'm not sure I really have a stronger opinion about what to do.Twospot
I also have never heard a .net programmer use any type of prefix. I've been intending to learn c# for 4 years now and still haven't done it. Once I do (it's inevitable), maybe I'll change my habits in VBA too. Or maybe VBA will dead by the time I actually learn C#. :)Hypochondriac
H
6

I would say that this kind of Hungarian notation is the root of all evils in almost every language. Some people say it is handy for extremely dynamic languages. But no, I think that prefixing the type-abbreviation onto a variable name is redundant in 99% of all cases and just leads to ugly code.

see Why Shouldn't I Use Hungarian Notation?

Hostelry answered 3/4, 2010 at 23:2 Comment(3)
My problem is that there isn't an easy way to see the type in VBA. Am I just missing it? Or maybe I should be using better names?Twospot
Well surely name will never be an integer and count will never be a string.Hostelry
+1 Can't argue with your point that Hungarian notation leads to ugly code.Morentz
T
5

I'd advise going for something a little higher-level than just types so that you can see what the purpose of things are. Thus, instead of calling something a string, call it a name or an address, and instead of an int, call it a count or a coordinate or ...

(I prefer to use suffixes to prefixes, but that's a matter of style and taste.)

Tejeda answered 3/4, 2010 at 23:4 Comment(3)
Sounds interesting. Would you mind giving me some examples? :-)Twospot
+1, this is what Joel takes about here joelonsoftware.com/articles/Wrong.htmlHostelry
@KennerL90: I'm utterly terrible at VB. The last time I wrote any kind of Basic was back in 1991 and in a totally different dialect. But the idea is that instead of calling a variable f_dwX (object-field double-word-type x) you instead call it x_coord; the purpose of a variable is important, and more stable, than its type.Tejeda
H
5

I always use one and two letter prefixes in VBA. I'm sure I'm the only one that's going to admit that, but I figured somebody needed to be the contrarian.

Of the 18 million lines of VBA code I've written, I've collaborated on about 1,000. If nobody else sees my code, then I'm free to use a convention that I like. If someone else will be working on your code, you should agree on a convention.

I like that it lets me keep my variable names shorter. I could use FileNumber and FileName or I could use lFile and sFile. I don't find one more or less readable than the other. It also helps me use reserved words as variables. If I want to use Replace as a variable name, I can't. But I can use sReplace or lReplace.

Hypochondriac answered 4/4, 2010 at 0:39 Comment(2)
Thanks for the response. The thing about the responses is that I've never seen anyone use hungarian in .Net code, but also, I've NEVER seen anyone NOT use hungarian in VBA. So I'm surprised that you are the only one that responded to say you use it in VBA. I appreciate all the answers, but I'm not sure I really have a stronger opinion about what to do.Twospot
I also have never heard a .net programmer use any type of prefix. I've been intending to learn c# for 4 years now and still haven't done it. Once I do (it's inevitable), maybe I'll change my habits in VBA too. Or maybe VBA will dead by the time I actually learn C#. :)Hypochondriac
M
0

If the style at your company is set to use Hungarian notation then there is no problem using it - a policy is a policy. There are plenty of tools that help enforce coding naming conventions (like Stylecop for C#) so you can move on if you are allowed.

Basically having standards is a good idea - but what those standards are depends on the company you work for. If you have some authority you could attempt to impose the standards that MS are currently promoting - but if you have a lot of legacy code that will involve a lot of expensive refactoring with little material benefit.

I would recommend that you move away from Hungarian notation for new projects (possibly by utilising a code analysis tool) but be pragmatic about legacy code.

Morentz answered 3/4, 2010 at 23:5 Comment(2)
Yea, the only thing worse than a Hungarian project is one that is half-Hungarian and half-not. Be consistent no matter what.Hostelry
Got to agree with the sentiment that consistency is key.Morentz

© 2022 - 2024 — McMap. All rights reserved.