views:

62

answers:

3

Mostly because of reading this site, I'm trying to move away from Hungarian Notation. Or I guess the improper (system) Hungarian.

I can figure out a better way to name most data types, but I don't know what to do with objects. What would be a good naming convention for objects? I use objRS for recordsets now. Would I just use rs?

+1  A: 

I would use customerRecordSet or just customers.

Do you really need to remind yourself that it's an object? Everything is an object, for the most part.

Do you really need to remind yourself that it's a recordset? Once you see yourself treating it like a recordset, it will be pretty clear that it's a recordset.

John Saunders
Bingo. It is not very useful to put the type of a variable into its name anymore, because in any IDE (and for that matter, the good text editors), you can go to the definition with a single keystroke.
Mike Daniels
Forget a keystroke: just hover your mouse. Or for that matter, good coding styles now suggest smaller methods and classes such that the definition is nearby anyway.
Joel Coehoorn
+1  A: 

You tagged your question vb, but your weren't specific as to which vb and the answer here really depends on what version of vb you're working with.

Systems Hungarian notation still makes a lot of sense when you're working with the old vb6 or vba languages. This is because it's a looser type system and you don't get much help on the data types from the IDE. The type information provided by the hungarian prefix is very valuable.

For newer (.Net-based) code, don't use a prefix at all. The .Net Framework likes strongly-typed code and Visual Studio will give you a lot more help when you need it. You just don't have to go around reminding yourself that a variable is an object or an integer or a recordset or anything else; it's immediately apparent in your development environment without any prefix.

Joel Coehoorn
Well, I guess I use .Net mostly, but I also use VBA. It looks like most of the threads on here say not to use Hungarian anywhere, even VBA. Do you disagree with that? Your thoughts give me something to think about.
EdOxH
I do disagree. I think hungarian had a purpose in places where type information is not otherwise easily obtained, but for most modern environments this is no longer the case.
Joel Coehoorn
Also, see my answer to this question: http://stackoverflow.com/questions/309205/are-variable-prefixes-hungarian-really-necessary-anymore/309232#309232
Joel Coehoorn
@Joel: frankly, I wonder if Hungarian is as important if you're using classes, or at least smaller functions and subroutines in VB6/VBA.
John Saunders
vb6 doesn't really have classes
Joel Coehoorn
A: 

Create names that are descriptive of the objects and of your usage of them. The names and the context (not to mention the intellisense) should be enough to remind you of their types as you look at them.

On that note, I feel your pain. I learned VB6 in college and was taught hungarian notation where a variable for a first name would be called strFirst. As I moved over to .NET and C#, I was not quick to let my old habit go even though the accepted standard for C# did not include such notation. I quasi-conformed (or at least, that's what I told myself) by shortening the notation to a single character, so first name would be stored in a variable called sFirst. I told myself I needed that character so I would know what my variable type was. But you know what? It reinforced another bad habit, in that my variable name was not descriptive enough. What is sFirst? First of what? Now, I could have obviously made the variable name more descriptive and kept the hungarian notation, but for me the one bad habit (the notation) went hand-in-hand with the other (abbreviated variable names).

Eventually, and it was probably less than a year ago, I bit the bullet and started writing code the "proper" way. The notation is gone, my names are more descriptive, and (best of all) I have no issue with looking at code and knowing from name/context what type of variable I'm working with and there is also no ambiguity of knowing what the variable refers to. I say let go, you'll not miss it.

Anthony Pegram
Thanks. That is exactly right. I was taught Hungarian. Hell, I was even taught Hungarian in .NET. Changing is taking some time. I'm curious, would you drop Hungarian in VBA too? Not sure if I need to maintain two separate naming styles (.Net/VBA).
EdOxH
I would say yes. I did some intermittent VBA coding for many years, and I think descriptive naming is just as relevant there as it is in .NET win-/webform applications. It's a good habit. To be honest, I now find the code I wrote before dropping the old habit to be an eyesore!
Anthony Pegram
I think you can do descriptive naming _and_ hungarian notation. Just because they went together for your doesn't mean they do for everyone, and it served a useful purpose among older IDEs and loosely typed languages where type information would otherwise be somewhat muddled.
Joel Coehoorn