Naming Conventions


The most important thing about a naming convention is that you should have one. If you name each variable according to the whim of the moment, you’ll not only end up with strange names that you won’t remember later but you’ll also spend your mental energy dreaming up clever variable names instead of clever algorithms. A naming convention should be automatic. Two people using the same convention should come up with the same variable names a high percentage of the time. That might be unrealistic in practice, but it’s a worthy goal.


In reading working code and programming books over the years, I’ve seen a lot of styles, many of them well thought out and helpful to the development process. I’ve also seen bad variable names make some good code hard to read. Frequently, however, wretched naming conventions go hand in hand with wretched code. And one of the most wretched conventions is offered to you free by the Visual Basic environment.


When the environment offers default names such as Form1, Control2, and Text3, just say no! I first encountered this bizarre behavior when I was invited to participate in a usability test of Visual Basic version 1 several months before its release. For my first button, Visual Basic helpfully provided the caption Command1 and the variable name Command1. I didn’t understand the difference. How could anyone imagine that I would ever want a button with the caption Command1? And the idea that I would want a variable named Command1 seemed only slightly less ridiculous.


I still find the default variable names mildly annoying and wish that Visual Basic had options to always leave the Caption, Text, and Name properties blank. You


I’ve made a similar error in Declare statements, thinking that, because I put ByVal with the first parameter, I didn’t need it with the second:

Declare Sub Line Lib “MyDll” (ByVal x As Integer, y As Integer)

Some programmers avoid this problem by declaring every variable on a separate line:

Dim c As Integer
Dim i As Integer
Dim h As Long

That’s a bit too extreme for me. I salute the intention, but I’m satisfied with this:

Dim c As Integer, i As Integer, h As Long

won’t see this terrible convention in the controls I create later in the book. I strongly recommend that you do the same in your own controls. When I insert one of Visual Basic’s controls, I immediately set the Caption (or Text) property and then give the control a reasonable name. It’s tempting to leave the default name on static labels that will never be accessed by name, but I resist even that. Instead, I put my static labels in a control array. This saves a little memory, and if I copy and paste an existing label, Visual Basic automatically names it and adds it to the array.


FLAME In writing this book, I read a lot of Basic code from many programmers. I’m tempted to publicly curse all those who post code (including large programs) to the public domain using the write-only naming convention provided by Visual Basic. Folks, it is very difficult to interpret your code. But I suspect that my curse is unnecessary. If you haven’t already paid the price, you will when you try to go back and modify your own code a year from now. The convenience of whipping out code this way is not worth the pain of trying to maintain it.