Naming Conventions – How to Avoid Using Hungarian Notation

hungariannaming

I've seen arguments for and against Systems Hungarian.
For some years I've been working on a legacy project that uses this system by naming every variable, function with a prefix of the variable type eg (strName, intAge, btnSubmit etc) (I know the original Hungarian Apps prefixes by the kind of variable, not the type).
I'd like my next project to abandon it fully, but I do find it harder to name similar things uniquely without resorting to it.

Lets say I have an webform for collecting email addresses and storing them in a database table, and button which calls the function which save the address to the db.

If I'm using Hungarian style notation, I might call the box txtEmail the button btnEmail and the value contained in the textbox strEmail. I might then use a function storeEmail(strEmail) to store the email. I have a clear convention here, it's obvious what each variable is.

What would be the best practice for naming these variables

  • without resorting to Hungarian Systems,
  • without making them over long or confusing
  • and with a clear convention to use across the whole of my project?

Best Answer

Your last point is the most important - whatever you do you need to be consistent across your project and with your colleagues. There are two main ways to acheive consistency and if possible you should use both. Firstly use a tool to check naming conventions at build time. In the .Net world StyleCop would be a good example of such a tool. The second way to get consistency is to have peer reviews of all code, so that you can all keep a look out.

Your other two points seem to be asking about an alternative. I'm not sure you need an alternative; the point about Hungarian no longer being popular is that it used to be used to descibe the type when the type system and tools were a bit, shall we say, less strict. I.e. if you were programing in C and passing pointers around the only way to keep track of the type was by using Hungarian. Now, if you are using a language like C# or Java you will not be using pointers (or very rarely), so the need for any kind of Hungarian goes away. Also, modern IDEs let you see the type very easily by hovering over the variable or at worst using some short cut to see the original declaration. So, I don't think you need any kind of notation, just name the variable by what it does. If it's an email address just use "email" or "emailAddress". If it's a customer name just use "customerName" etc.