Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube: Should magic numbers be allowed in java constructor enums

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

like image 877
DEG Avatar asked Jun 24 '15 07:06

DEG


People also ask

What is a magic number why are magic numbers problematic?

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.

Is a magic number consider replacing it with a named constant?

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.

Should enum fields be final?

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.


1 Answers

It will be fixed in version 3.4

See this issue on SonarSource : http://jira.sonarsource.com/browse/SONARJAVA-1117

like image 97
Dams Avatar answered Oct 02 '22 04:10

Dams