Technically, yes, there is nothing wrong with the code. The code shows that the color code for each type of enumeration is based on the constructor (for example, red - 42). But the squid rule dictates that it can be "confusing" when someone tries to debug code (in particular, a large piece of code).
This is taken from the squid rule.
Using magic numbers may seem obvious and simple when you write a piece of code, but they are much less obvious and understandable during debugging.
This is why magic numbers must be demystified by first assigning explicitly named constants before use.
-1, 0, and 1 are not considered magic numbers.
So in your code you can do something like this.
public enum Color{ public final int RED_CODE = 42; RED(RED_CODE); public final int code; Color(int colorCode){ this.code=colorCode; } }
or may also disable the rule: D
source share