Java Notes: Style: No magic numbers - use named constants
No magic numbers - Use named constants.
Programs often have numbers embedded in them that tell how big arrays are,
how many times to do things, conversion factors, etc. These should never
be written in the body of the code. Define "named constants" in upper case
using the final
keyword.
Motivation
- Vastly improves readability.
- Ensures consistency and accuracy.
- Provides a single point of change.
Typically these are defined as static constants, for example:
. . . public static final int MILES_PER_KILOMETER; . . .