Incremental Java
Descriptive Variable Names

Descriptive Variable Names

Java has rules for identifiers (variable names are identifiers). If you don't use those rules, then Java complains (we'll explain what this means later on).

You should write Java variable names using the Java style. Java does not complain if you don't use Java style, but it makes your code look more like Java code written by others, and consistency is nice.

Java style only tells you how variable names should appear. It doesn't tell you what names to pick. You still have to pick the names.

Short Names Are Bad

FORTRAN was a popular language in the 1970's, and still is used even today. The early versions of FORTRAN had a rule that made good variable names difficult. Variable names could only be 7 characters long.

What's worse, FORTRAN programmers were usually scientists such as physicists, mathematicians, chemists, etc. They wrote programs that were translations of equations in their field of science. For example, there's the famous F = ma (force is mass times acceleration).

Variables in science are often a single character long. This makes it easier for professors to write equations, but it makes it harder for the average person to understand.

People who studied programmers realized that one letter variable names were often hard to undestand, yet, it was very common. Variables names like X1, X2, and T were used all the time. Now, if these variables stood for letters in equations, maybe that would be OK.

However, as a programmer, you're not likely to use equations. Even if you are, it's worth making names longer. For example, you should use force, mass and acceleration instead of F, m and a.

You can abbreviate if you want, and are sure it's clear. For example, you can use acc instead of acceleration.

Abbreviations

You see abbreviations such as acct for account or num for number. You don't want to abbreviate too much because it can be difficult to read. For example, if hMT stood for "has multiple threads", it would be easier to type, but not nearly as descriptive as hasMultipleThreads. Someone else reading it would have no idea what it meant, and you'd have to explain it to them. You want to make variables as descriptive as is convenient.

You end up typing a lot more letters for descriptive variable names, but most people really do get used to it. Initially, it's a pain, but later on, it's not that bad. If you think about it, you type far more words when you write an essay for a class than you do in a program.

Descriptive Variable Names Only Go So Far

Descriptive variable names really make reading code a lot easier. But the reality is that some variables are hard to describe accurately. In general, you want to be descriptive, but keep your variable names from about 8 to 20 characters.

Perhaps you want to keep track of the temperature of a heat bath, when there is 3% chlorine in it, under ultraviolet light. You might think of a variable name like tempOfHeatBathWith3PctChlorineWithUVLight. That's reasonably accurate, but it is really long to write, and so you might just be better using temp (though temp is very popular for temporary variable), and then add a comment to describe what the variable means.

Then there are some concepts that are simply difficult to come up with good, accurate variable names. If that happens, just do the best you can. Later on, you may think of a better name, at which point, you can just change it.

Don't be afraid to change a variable name if you come up with something better. Many editors give you search-and-replace features, and some are very sophisticated, replacing only when it makes sense.

Coming up With Descriptive Variables Names

Let's try an exercise. I'll give a list of things we need descriptive variables for, and you come up with descriptive variable names.

Have you thought of some? Here are some suggested names:

Depending on context or personal preference, you may want to shorten the name. For example, you may simply want to saw width instead of rectWidth.

Condition Variables

As you write a program, you're likely to keep track of certain conditions. For example, you may need a variable to keep track if it's raining or not, if it's too hot or not, if someone passed an exam or not, and so forth.

Variables that have the value true or false have boolean type. These are often called condition variables.

These variables are often called flag variables. The name probably comes from people who use flags to direct airplanes or convey some information. Who knows?

However, avoid using flag for condition variables because although it sounds somewhat descriptive, it's not. After all, what does flag mean when the value is true? What does it mean if it's false?

Try to name condition variables like a question. For example, isRaining, isTooHot, hasPassedExam, etc. You start with a verb like is, has or maybe contains. That way, people know what it means for that variable to be true or false. flag tells you too little.

Keep variable names positive. Instead of isNotFull, use isEmpty, or use isFull (we can get the opposite of isFull by using logical operators, which we'll explain in another lesson). We'll talk more about how to use condition variables once we get to real programming.

Summary

Make variable names descriptive. That way, anyone who reads it can guess its meaning.

Descriptive variable names should be between 8-20 characters. This is not a restriction, but just a guideline for the length of variable names. If it's too long, then it's really hard to type. If it's too short, it may not be descriptive enough.

When you start off, it's going to be hard to come up with descriptive variable names. Do the best you can. Keep thinking of better names, and see how other, more experienced programmers do it.

Use the Java style of writing variable names. This means that if the variable name is two or more words long, capitalize all but the first letter of each word. Thus, you would write, meanTempInAus to stand for the mean temperature in Australia, instead of mean_temp_in_Aus. This makes your code look more consistent with pre-existing Java code.

You can abbreviate words in variable names, but be consistent (e.g., num for number, but don't use both num n and no as abbreviations).

Coming up with a consistent way of naming variables is a key to writing programs that other people can use. It gives your code predictable names that people can guess. We'll look at this in more detail once you begin to write programs.

For now, do the best you can.