Constants

Purpose

Many different tools are needed in the process of getting the problem and solution organized. This section describes the role constants play in program readability and maintenance.

Description

Constants provide a way to give a symbolic name to a piece of information that you do not intend to change in the course of program execution. For example if you are writing software for a machine that produces 50 parts in every batch of parts it manufacturers you might create the constant:

const PartsInBatch = 50;

Constants can also be created as plate symbols using the Constant Editor such as shown below:

The equals sign is used instead of the assignment operator to convey that these two things, PartsInBatch and 50, are equivalent. There is no ?copy? activity that occurs in the program at run time. Using a constant is just as efficient as using the explicit number in terms of the resulting program speed however the program is much easier to understand. The following statement may appear in the application program:

if NumberOfPartsMade = PartsInBatch then......

It? is quite clear that the decision has something to do with the batch being finished. If the number 50 was there instead that would not be as clear. The place where the use of constants makes a dramatic difference is when he number of parts in a batch is changed to 75. The number of parts in a batch may be used many times and in many places throughout the program. If an explicit number was used the program would have to be searched for all of the 50s, making sure that the particular 50 related to the number of parts in a batch (as distinct from some other quantity which might happen to also have the value of 50) and be change to 75. Subtle problems can occur one of the 50s was missed. There?s also a tendency for 49s and 51s to appear, particularly in loop definitions, and these are easily missed. However if a constant named PartsInBatch was used the only changed needed would be the constant declaration:

const PartsInBatch=75;

All of the places that were concerned with the number of parts in a batch are now automatically updated. The places where there might have been 49s or 51s now become PartsInBatch-1 and PartsInBatch+1. It may appear that this is less efficient because of the additional operations taking place to calculate that value however this is not a problem. The compiler is aware that the expression PartsInBatch+1 is composed of values all known at compile time so the compiler itself performs the calculation once creating the 51s and 49s (or 76s and 74s if the PartsInBatch is 75). Using constants allows you to centralize a machine attribute and derive other values from it to manage application information. If a numerical value is used in more than one place for the same meaning use a constant to insure these two values ?stay in synch? in the event of a future change.

An important place for constants is in array bound declarations. Arrays are often used to store information about the motion system during data collection. It is very important that an array is not accessed beyond bounds. Accordingly whatever procedure is filling the array should use constants to indicate the range of filling which are the same constants used to define the array size. This insures that as the array size is altered that the filling operation is automatically altered as well to insure consistency. Note that symbolic names may be typed into the lower bound and upper bound fields in the variable declaration dialog box: