patternjavaCritical
'100' is a magic number
Viewed 0 times
magic100number
Problem
Magic numbers are bad... I totally agree. But there's one magic number I find hard to fix:
'100' is a magic number.
Consider this code:
SonarQube will raise 2 violations, one for each use of 100. I could replace 100 with a constant variable, but what would be a good name for it? Is that really a good idea? If somebody ever changes its value that will be a disaster. I could also just add
What is the best practice for dealing with the magic number 100?
UPDATE
From the answers, the most useful bit for me was this:
Practical Magic Number rule: A literal is a not a magic number if the most meaningful variable name for it is the same as the spoken name of the literal.
So by this logic, 100 is NOT a magic number.
However, to make the violation go away, I decided to replace the 100 with a constant:
In my real project there are many lines using 100, and if I put
Not sure if there will be any real benefits using this constant. A small one may be that when I do
'100' is a magic number.
Consider this code:
public double getPercent(double rate) {
return rate * 100;
}
public double getRate(double percent) {
return percent / 100;
}SonarQube will raise 2 violations, one for each use of 100. I could replace 100 with a constant variable, but what would be a good name for it? Is that really a good idea? If somebody ever changes its value that will be a disaster. I could also just add
// NOSONAR to all the lines that use 100.What is the best practice for dealing with the magic number 100?
UPDATE
From the answers, the most useful bit for me was this:
Practical Magic Number rule: A literal is a not a magic number if the most meaningful variable name for it is the same as the spoken name of the literal.
So by this logic, 100 is NOT a magic number.
However, to make the violation go away, I decided to replace the 100 with a constant:
public static final double HUNDRED = 100; // change it and I'll kill youIn my real project there are many lines using 100, and if I put
// NOSONAR on all those lines that might cover up other potential problems that other developers might inadvertently add later.Not sure if there will be any real benefits using this constant. A small one may be that when I do
git grep 100 I see a lot of matches from resource files in the project, while git grep HUNDRED turns up just the the Java code that uses this.Solution
Speaking as a human programmer (i.e. I am not Lint software), your use of "100" there looks fine to me.
Wikipedia has an article (without citations) titled Accepted limited use of magic numbers: IMO your "100" is in the same category as these other "accepted" magic numbers.
This Wiki describing magic numbers says two things.
Firstly,
Practical Magic Number rule: A literal is a not a magic number if the most meaningful variable name for it is the same as the spoken name of the literal.
That's applicable here: you're looking for a named constant like
Secondly, it also suggests loading "magic" numbers (e.g. a "discount rate") from a configuration file:
Note that though this example code carefully loaded
If you use "100" instead of
IMO the only benefit to using
Wikipedia has an article (without citations) titled Accepted limited use of magic numbers: IMO your "100" is in the same category as these other "accepted" magic numbers.
This Wiki describing magic numbers says two things.
Firstly,
Practical Magic Number rule: A literal is a not a magic number if the most meaningful variable name for it is the same as the spoken name of the literal.
That's applicable here: you're looking for a named constant like
HUNDRED or CENTUM.Secondly, it also suggests loading "magic" numbers (e.g. a "discount rate") from a configuration file:
static final double DISCOUNT_PERCENT = getProperty( "sales.discount_percent" );
static final double DISCOUNT_FACTOR = 1 - (DISCOUNT_PERCENT / 100);
// ...
salePrice = DISCOUNT_FACTOR * regularPrice;Note that though this example code carefully loaded
DISCOUNT_PERCENT from a configuration, the "100" used to calculate the DISCOUNT_FACTOR is hard-coded.If you use "100" instead of
HUNDRED, it's easier for a programmer to understand, and to verify that it's correct.IMO the only benefit to using
HUNDRED is to find the several methods which use the same magic number (in your example it's used by getPercent and getRate).Code Snippets
static final double DISCOUNT_PERCENT = getProperty( "sales.discount_percent" );
static final double DISCOUNT_FACTOR = 1 - (DISCOUNT_PERCENT / 100);
// ...
salePrice = DISCOUNT_FACTOR * regularPrice;Context
StackExchange Code Review Q#44047, answer score: 82
Revisions (0)
No revisions yet.