#303 MagicNumber over-enthusiastic

Future
closed
nobody
Check (274)
5
2012-10-10
2004-06-30
Benoit Xhenseval
No

Hi

As suggested by Oliver, I am raising an RFE to ask to
make the MagicNumbers test a bit 'smarter'

Should this code:

public class A {
private static final int WIDTH_FACTOR = 256;
private static final int ASSET_DESCRIPTION_WIDTH =
50 * WIDTH_FACTOR;
private static final int ID_SBL_WIDTH = 10 *
WIDTH_FACTOR;
...
}

generate the following checkstyle violation:
'50' is a magic number.
'10' is a magic number.

It seems a bit over-the-top since it is in a private static
final int...

I am using the latest checkstyle.

Thank you,

Benoit

Discussion

  • Gunter Blache
    Gunter Blache
    2004-08-09

    Logged In: YES
    user_id=1095933

    This is a useful idea. I recently had a similar problem myself:
    private static final INSETS = new Insets(4, 4, 4, 4);
    where checkstyle complained four times about the four fours

     
  • D'Arcy Smith
    D'Arcy Smith
    2005-05-29

    Logged In: YES
    user_id=554117

    also cases such as this should be handled:

    public static final int X;

    static
    {
    X = 5; // do not issue a warning for this
    }

     
  • Lars Kühne
    Lars Kühne
    2006-01-13

    Logged In: YES
    user_id=401384

    The MagicNumber check has been less aggressive since release
    3.5 (sorry for not updating this tracker item immedaitely),
    and the following code no longer generates any report:

    public class A {
    private static final int WIDTH_FACTOR = 256;
    private static final int ASSET_DESCRIPTION_WIDTH = 50 *
    WIDTH_FACTOR;
    private static final int ID_SBL_WIDTH = 10 * WIDTH_FACTOR;
    private static final Insets INSETS = new Insets(4, 4, 4, 4);
    }

    The static initializer code mentioned in one comment is not
    covered, and I'm not sure that this should really be
    allowed. If this is still an issue please open a new RFE.

    Closing this one, as the original request has been implemented.