The Great Hungarian Debate

I’ve discussed this in 2005. Then again in 2009. I guess it’s time to revisit the issue, mostly because Jordan says it’s time to say goodbye to hungarian notation.

I’ve probably said everything I need to on the subject, so no long diatribes here. However, I decided that I was going to experiment with coding without Hungarian and see what I thought. A little experimentation can’t be bad. I have a choice. I can be stubborn and not learn anything or I can force myself to try something different and maybe I’ll be the better for it. There was a time when I never coded custom class modules. Now you can’t get me to shut about them.

Here is the first procedure I coded in my experiment:

Public Sub ReformatDrivers()

Dim Cell As Range
Dim Found As Range

For Each Cell In Sheet1.Range("A2:A133").Cells
Set Found = Sheet2.ListObjects(1).DataBodyRange.Find(Cell.Value, , xlValues, xlWhole)
If Not Found Is Nothing Then
Cell.Offset(0, 1).Value = Sheet2.Cells(Found.Row, 2).Value
Cell.Offset(0, 2).Value = "'" & Sheet2.Cells(Found.Row, 1).Value
Cell.Offset(0, 3).Value = Found.Offset(0, 1).Value
End If
Next Cell

End Sub

Not exactly a barn burner, I’m sure you’ll agree. I hated every minute of it. I hate reading it right now. I’ve struggled to pinpoint why it displeases me so, but I have a theory.

It’s hard to tell the difference between keywords and variables. For Each Cell are all one syllable words with the first letter capitalized. The color distinction actually shows up better on this blog than it does in the IDE. I could change my color options in VBA so it stands out a little better.

There’s no requirement to make my variables proper case and thus hard to distinguish. I could code For Each cell and make my variables stand out because they’re lower case. But there is a substantial advantage to using capital letters – the IDE fixes your caps and tells you if you have a typo. So I want to have at least one capital letter to get that benefit. I could use camel case for two syllable words, like fileName. Do I have to always avoid one syllable words?

As I’ve mentioned in the past, I don’t use data type prefixes in other languages. But like this experiment, I don’t really like the variables I use when I code Ruby and I think it’s for the same reason. The difference is that my Ruby IDE doesn’t fix caps and, maybe more importantly, everything about Ruby is new and novel and foreign so on the scale of strangeness, all lower-case variables don’t really rate.

Another advantage of data type prefixing is being able to use reserved words. For my experiments, if I want to use a reserve word I’m going to tack on an underscore. When I want to code Dim lEnd As Long, I will instead use Dim End_ As Long.

I haven’t made a userform yet, but there is a problem that I’m not sure how to solve. Most of my controls have labels and any control with a label is named the same as the label. The textbox tbxSearch has lblSearch. The combobox cbxCustomer has lblCustomer. There’s real value in that and I’m not sure how to get away from it. Another problem with userforms are class properties. When I start typing Me.tbx I know I’m getting a textbox. But if my textbox is called CustomerName and I have a property of the userform class to hold a customer name, how do I distinguish them without the tbx? That’s not a rhetorical question, I really want to know how people do it.

I’ll keep writing new code without data type prefixing until I can’t take it anymore. And, of course, I’ll keep bitching about right here.

Posted in VBA

13 thoughts on “The Great Hungarian Debate

  1. After many years of learning VBA, I have recently started VB.NET. I have yet to see a forum or book that uses Hungarian notation for VB.NET (or any language other than VBA for that matter). I don’t feel qualified to answer your question about user forms but I look forward to what other contributors have to say.

    What I am interested in is why you chose Ruby as a second language? You are clearly an expert in VBA, I would’ve expected you to use VB.Net to support that.

  2. Keep the prefixing for controls, would be my suggestion. It’s still my own practice, come to think, even though I gave up on prefixing by type anywhere else a couple of decades ago. When the type of thing being referenced can’t be easily seen, the prefix gives a really useful hint.

    Actually, I do still use the occasional prefix, more in the original Simonyi “Apps Hungarian” spirit, such as “idx” for indexes (indices?). In general, though, I try to keep code routines small enough that type declarations are always easily visible.

  3. As you can tell from my response in Jordan’s blog, I agree with everything you say Dick!

  4. Does anyone know why someone decided to call it “Hungarian” notation?

    Was is someone who loved the movie “The Usual Suspects”?

    Was it Keyser Söze himself?

  5. @Mike There is an excellent article referenced from JPs website called ‘Making Wrong Code Look Right’ which answers your first question. Although you might want to skip down the page to subheading ‘I’m Hungary’ to find it.
    “Hungarian notation was invented by Microsoft programmer Charles Simonyi…”

  6. So I started writing a response here. But then, well, it became a manifesto. In fact, I’ve now spent three hours writing it.

    So… I think what I’m going to do is break it into pieces and post it on my blog.

  7. PeterB, that was an excellent article. I had only ever heard of prefixing variable type… not function. Prefixing with a variable’s function makes a lot more sense as you’re adding something the code doesn’t make explicit.

  8. I chose Ruby as a second language for a couple of reasons. First, I’ve been reading the Basecamp nee 37signals blog for a while and I’m a fan. DHH at Basecamp developed Ruby on Rails and released it to the world. Second, I wanted a departure from VB; something different enough to give me a new flavor. I dare say I write better VBA because I learned some Ruby, but I’m not sure that would be the case with VB.Net.

  9. Having programmed since punch cards, I still use “Magyarstilus” for all of my code. The reason came when I first learned Pascal on an Apple II+, I had to distinguish data types beyond real and integer. I still use x,y,z for real and i,j,k for integers because of Fortran conventions. I first saw the Hungarian style of programming in Fortran(!) but for slightly different reasons. The prefixing helped to determine scope, dynamic vs. static and whether you were passing pointers (dummy variables) or specific global values.
    With OOP, I was glad I already thought that way and continued on. Of course it is less necessary with all the nice IDE features for all programming languages, but my brain still works a helluva lot faster with a structured prefixing. What I would much rather see is a Google sort of approach in an IDE where your preferred set of objects and methods gets priority or you can easily filter subsets of them (I know you can do it, but it’s not to the point of intuition it could be.

Leave a Reply

Your email address will not be published. Required fields are marked *