Variables that don't Vary
In many programming environments, there is a need for variables that don't actually vary. For example, if a program calculates sales tax quantities, it must be given the sales tax rate for the specified community. If this is done as a number in the code, it might look like the following.
taxRate = 0.075;
However, if this number is used as is and written into all the calculations and assignments throughout the program, there are a couple of problems:
- When a person reading the code gets to line 567 and sees this number in some calculation, there is no way to know what it means unless it is appropriately commented,
- If the tax rate changes in the community, the programmer will have to search through the entire program code to find all the numbers and replace them with new ones.
This does not represent good readability or maintainability.
The solution is to create constants. Constants are name identifiers that hold, well . . . , values that don't change. They represent superior self-documenting in the program, and if a constant changes in the future, the maintenance process is very simple.
You have seen constants used in your programs already. They should be the first things to go immediately after a class is declared. Here are some examples. The const keyword is what makes these items constant.
const short SCREEN_COLOR = COLOR_GREEN; // creates a color constant
const char SPACE = ' '; // creates a space constant
const double TAX_RATE = 0.075; // creates a tax rate constant
const int MAX_ITEMS = 250; // limits a program value
As you have observed previously, the convention for using constants is to capitalize them and/or place underscore characters between words used to make up the constant name; this is a habit you should get into. As you have also observed previously, after the constants are defined, the local member variables are also declared, but those quantities vary; your constants won't.
In the course of discussing constants, there is also the opportunity to bring up the concept of literal data. A literal is the actual value of something, instead of a variable representing it. In the following, each of the assigned quantities are literals. The 74, the 4.95, and the character X are all literal values, and each are assigned to variables.
myAge = 74;
mySalary = 4.95;
myMiddleInitial = 'X';
For purposes of constants, you are actually replacing literal values with readable names. Consider the following function call; again, don’t worry if you haven’t worked with functions yet, the essence should be clear.
You could make a function call, such as:
someFunction( 17, 22, “Result: ”);
Anyone reading this could would have to guess, or go look up, what the numbers 17 and 22 mean.
However, consider the following code:
someFunction( X_DISPLAY_POSITION, Y_DISPLAY_POSITION, “Result: ”);
There should be little doubt now what the numbers mean, and what information is being sent to this function. The numbers 17 and 22 are commonly called “magic” numbers because no one knows where they came from. Obviously, magic numbers are not a sign of a professional programmer.
Any data can be in literal form. In the following, the value "Bill" is said to be a string literal, assigned to a string variable, and the literal value “myfile.txt” is assigned to the constant FILE_NAME. Once again, these are actual values placed in the programming code.
myName = "Bill";
const string FILE_NAME = “myfile.txt”
The use of constants provides powerful support for quality, readable programming. While there may be some use for literal values such as when you are displaying a particular string to the screen, if a certain numerical or textual value is to be displayed or manipulated more than one or two times, it should probably be made into a constant as needed, or a variable if the literal values are subject to change. And from the previous chapters, we continue to maintain the rule that there are no literals in parameter lists unless they are self describing. An example of this would be:
printStringJustified( "This is a string", DATA_BLOCK_SIZE, "LEFT" );
In this case, the string literal ("This is a string") is the data to be output and it would be redundant and unnecessary to put this into a variable or constant unless it was repeatedly used; also creating a variable or constant for the justification quantity ("LEFT") would again be redundant as you would most likely label it the same thing (e.g., LEFT) as the literal text. For these self-identifying literals, no constants or variables are needed. However, for the DATA_BLOCK_SIZE, if your function just had a 25 in it, a person reading the code would have to stop and figure out what this number was and what it was for. In that case, you really must use a constant or some kind of self-documenting quantity.
This would be a golden opportunity to create another program that uses constants but one of the videos you have already viewed that uses formatted I/O provides a great example of the use of global constants. Watch Console I/O Step One, Console I/O Step Two, Console I/O Step Three, Console I/O Step Four, work through them again, and it will still help.