#436 MagicNumber includes static final variable mathematics

open
nobody
Check (274)
5
2012-10-10
2006-10-23
Andrew
No

It would be useful to be able to turn off magic
number checking on static final variables that
contain mathematical constructs e.g.
public final static int HALF_WIDTH = WIDTH / 2;

Note that I would not want to turn off for every 2
since I don't want to allow 2s in the code (for
example).

Discussion

  • Lars Kühne
    Lars Kühne
    2006-10-23

    Logged In: YES
    user_id=401384

    do you really want to turn the check off completely, i.e. allow

    public static final int EXTENDED_WIDTH = (WIDTH * 17 + 48) % 5;

    or do you simply want a different set of allowed numbers in
    mathematical constructs, so you can allow 2, but disallow 17
    or 48.

     
  • Andrew
    Andrew
    2006-10-24

    Logged In: YES
    user_id=1504432

    I would rather this than allow:
    public final static int WIDTH = 100;
    public final static int EXTENDED_WIDTH = 3; // This is the result of (WIDTH * 17 + 48) % 5
    which is allowed in the current system. This now removes the dependance on WIDTH, so if I change it, the code will
    probably break.

    The main problem here is that this check is very situation dependant - sometimes it is OK and sometimes it is not.
    One solution would be to be able to specify a variable name regular expression and a list of allowed numbers if the
    expression is matched so I could allow, for example, the number "2" if the variable name includes the word "HALF"
    or "DOUBLE". This would be more stable, and would force the programmer to think if they would rather include the
    regular expression in the check or create a new static final variable for the number (both of which require extra
    work).

    Andrew :)