Concerning rule squid: S109 Magic numbers should not be used
Shouldn't it be allowed to have numbers in the constructor of an enum in java? The code below shouldn't violate the rule in my opinion.
public enum Color{
RED(42),
GREEN(123456),
BLUE(666);
public final int code;
Color(int colorCode){
this.code=colorCode;
}
}
I'm using Sonar java plugin version 3.3
A magic number is a number in the code that seems arbitrary and has no context or meaning. This is considered an anti-pattern because it makes code difficult to understand and maintain. One of the most important aspects of code quality is how it conveys intention. Magic numbers hide intention so they should be avoided.
A magic number is a number in code with no clear meaning to the reader. Magic numbers should be avoided. They should instead be replaced with named constants. This helps with readability and maintainability.
Enum fields are treated like fields on any other type. Again, because enum values are constants, fields should be final variables, since they will not change.
It will be fixed in version 3.4
See this issue on SonarSource : http://jira.sonarsource.com/browse/SONARJAVA-1117
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With