4E. Choosing Variable Names


Avoiding the swamp: choosing sensible variable names

Choosing sensible variable names makes your program easier to understand. A variable whose value is the size of an array could sensibly be called size. Don't call it briefcase. A variable that holds a string that is the user's name might be called userName. Use common sense.

Choosing poor variable names confuses you and anyone anyone else who reads your program. If you put down a program one day and come back to it the next day, you might as well be another reader; you will almost surely have forgotten the details.

What not to do.

The following is a short list of some of the poor variable names that students have used programming assignment submissions in this course. Don't do it like this.

  • A student called a variable retrievedFile, when the variable was not any kind of file.

  • Variable charArr was not an array of characters.

  • Variable vert1 was not a vertex, it was an event. (We will see both vertices and events later.)

  • Variable physize was not the physical size of anything.

  • Variable elevations was one elevation.