No magic number check in definition of enumeration items

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

No magic number check in definition of enumeration items

Kai Werther
This post has NOT been accepted by the mailing list yet.
It seems the "Magic Numbers" check will be reworked to excluded the number "2" as magic number. Here I have an additional request, namely the use of "magic numbers" in the definition of enums. I consider enumerations also "constants", so the use of any number should be allowed here.  e.g.

enum CDataType {

     uint8(1),  // others left out ....

    private int byteSize;

    private CDataType(final int bSize){
       this.byteSize = bSize;


Here the use of the "magic number" 8 is fine, as it is used to define the properties of a "constant" enumeration item.  Also it is not possible to extract the "8" into a constant inside the enumeration, so any use of symbolic constants here would lead to highly more unreadable code.

We have used NOSONAR tags at these places in our code to silence the warning but it would be much better if the "Magic number" rule would not consider this a violation.

I hope you can follow my line of argumentation.