User Tools

Site Tools


guides:programstyle:constants

This is an old revision of the document!


Constants

If a constant used in a program has a meaning, or it is possible that this constant may be changed in the future, the constant should be named. In general there should be no magic numbers or numbers that have a meaning but are not named, in a program.

Assume you are writing a program involving sales tax. The county tax rate is fixed at 4.75%. This rate will not change for the duration of the program, but in the future the tax rate may change, so this should be a named constant.

Acceptable Example

const float TAX_RATE = 0.0475;
...
 
tax = price * TAX_RATE;

Unacceptable Example:

tax = price * 0.0475;

At times the same constant may be given multiple names. Assume you are writing a program where the maximum number of items a person may purchase is 20. In addition, if the purchase price is over $20, the consumer will receive a discount. Each of these has a different meaning, so

Unless otherwise instructed, a single prompt, the

guides/programstyle/constants.1595591604.txt.gz · Last modified: 2024/07/25 15:01 (external edit)