views:

149

answers:

4

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.

+4  A: 

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?

Earlz
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?
KennerL90
Well surely `name` will never be an integer and `count` will never be a string.
Earlz
+1 Can't argue with your point that Hungarian notation leads to ugly code.
amelvin
+4  A: 

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

Donal Fellows
Sounds interesting. Would you mind giving me some examples? :-)
KennerL90
+1, this is what Joel takes about here http://www.joelonsoftware.com/articles/Wrong.html
Earlz
@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.
Donal Fellows
A: 

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.

amelvin
Yea, the only thing worse than a Hungarian project is one that is half-Hungarian and half-not. Be consistent no matter what.
Earlz
Got to agree with the sentiment that consistency is key.
amelvin
+2  A: 

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.

Dick Kusleika
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.
KennerL90
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#. :)
Dick Kusleika