This question already has an answer here:
- When is a number a magic number? 3 answers
I want to avoid numbers used directly in expressions without obvious meaning. However, if a number is used only once, as an argument passed into a function, and that number can be easily changed without breaking the program, is that number considered a magic number?
For example, I have a function
double doSimulation(double alpha, double beta, unsigned count) where the parameters are to the simulation model. In my application code, I type in
double x = doSimulation(0.5, 0.2, 100) as part of the application logic, where the numbers 0.5, 0.2 and 100 are prone to change and not used anywhere else, i.e. they can be changed directly to improve the model.
In this case, are the numbers considered magic numbers? Is it redundant to declare
const double ALPHA = 0.5; const double BETA = 0.5; const unsigned COUNT = 100;
and use them only once, as the numbers are already named by means of function parameter?