Sigh...



  •  Just found this in my codebase:

     

        static final String ONEHUNDRED = "100";
        static final String TWOHUNDREDFIFTY = "250";
        static final String FIVEHUNDRED = "500";
        static final String SEVENHUNDREDFIFTY = "750";
        static final String ONETHOUSAND = "1,000";
        static final String ONETHOUSANDFIVEHUNDRED = "1,500";
        static final String TWOTHOUSAND = "2,000";
        static final String TWOTHOUSANDFIVEHUNDRED = "2,500";
        static final String THREETHOUSAND = "3,000";
        static final String FOURTHOUSAND = "4,000";
        static final String FIVETHOUSAND = "5,000";
        static final String TENTHOUSAND = "10,000";
        static final String HALF_PERCENT = "1/2%";
        static final String ONE_PERCENT = "1%";
        static final String TWO_PERCENT = "2%";
        static final String THREE_PERCENT = "3%";
        static final String FOUR_PERCENT = "4%";
        static final String FIVE_PERCENT = "5%";
        static final String TEN_PERCENT = "10%";
        static final String ONE_HUNDRED_THOUSAND   = "100000";
        static final String TWO_HUNDRED_THOUSAND   = "200000";
        static final String THREE_HUNDRED_THOUSAND = "300000";
        static final String FOUR_HUNDRED_THOUSAND  = "400000";
        static final String FIVE_HUNDRED_THOUSAND  = "500000";
        static final int INT_ONE_HUNDERD_THOUSAND = 100000;     // my favorite

       static final String ONE = "1";     //  runners up

       static final String TWO = "2";



  • The classic Const pattern. Is it actually used anywhere? Because I'm pretty sure you'll find more wtfs there.



  •  I've seen this extensively in code coming from developers either residing in or recently from India.  what gives? coincidence?



  • I really wonder what thought process leads programmers to think that's a good idea.



  • @mott555 said:

    I really wonder what thought process leads programmers to think that's a good idea.

     

    Sometimes an authority figure lays down the law that "magic numbers" can't be used anywhere in the codebase. A fine sentiment in general, but when said authority figure doesn't fully grasp the reasons for it, or enforces it the wrong way, you get things like this.



  •  for bonus points, INT_ONE_HUNDERD_THOUSAND is even misspelled.

     

    These are all deductibles and coverage amounts for insurance. Here's some sample usage:

     

     if ((dwellingCoverAmt.hasValue()) &&
                      (((Integer)dwellingCoverAmt.getObject()).intValue() < INT_ONE_HUNDERD_THOUSAND) &&
                      ONETHOUSAND.equals(allPerilsDeductAmt.getString()) && windDeductAmt.hasValue())

    // do stuff

     

     I'm pretty sure the thought process behind this, having been the culprit behind less egregious abuses of this sort, is copy paste style programming. Like, you're copying the basic logical structure of some other place in the code, where using constants actually makes sense. You see if(CONSTANT.equals(value)) and then copy that structure, not understanding the logic behind using constants.

     Hell, this code could even be redeemed. Instead of having "INT_ONE_HUNDERD_THOUSAND = 100000" it should have "DWELLING_COVERAGE_MAXIMUM = 100000. Then using a constant would actually make sense!

     

     



  • It's not rocket science. "FIVE_HUNDRED" is just as much a magic number as "500".



  • @Bumble Bee Tuna said:

    Hell, this code could even be redeemed. Instead of having "INT_ONE_HUNDERD_THOUSAND = 100000" it should have "DWELLING_COVERAGE_MAXIMUM = 100000. Then using a constant would actually make sense!

    So now TRWTF is possibly going to be in the future; because next year all deductibles go up by 5% so then

        static final String ONE_HUNDRED_THOUSAND = "105000";

    Don't say it doesn't happen.



  •  And you just know down the road someone's going to complain because ONETHOUSANDFIVEHUNDRED should be FIFTEENHUNDRED.



  • I don't see the obligatory

    static final String TWENTYFIVE_PERCENT = "12.5%"; // VAT percentage changed

    Also, why would you have commas in the string up to 10k, but not for 100k and above?


  • :belt_onion:

    @b-redeker said:

    @Bumble Bee Tuna said:

    Hell, this code could even be redeemed. Instead of having "INT_ONE_HUNDERD_THOUSAND = 100000" it should have "DWELLING_COVERAGE_MAXIMUM = 100000. Then using a constant would actually make sense!

    So now TRWTF is possibly going to be in the future; because next year all deductibles go up by 5% so then

        static final String ONE_HUNDRED_THOUSAND = "105000";

    Don't say it doesn't happen.

    You can be sure that if you have a constant "INT_ONE_HUNDERD_THOUSAND" it will be re-used by other developers for completely different calculations as well. And hilarity ensues


  • @mott555 said:

    I really wonder what thought process leads programmers to think that's a good idea.

     

    How this happens: the company adopts in the "no magic numbers" rule. This means that any numerical value must be replaced by a constant with a name that explains what it means. (For example, NUMBER_OF_SEASONS = 4, not because it might change in the future, but to indicate what kind of data you're working on.)

    Most likely, the company runs a source analysis tool that marks all the magic numbers in the code.

    The programmers read this report and decide to improve their code. However, they are either

    • lazy,
    • unimaginative,
    • unaware of the intent of the rule.
    • most likely all of the above
    Since the analysis tool doesn't look at the constant's name, this will improve the score while completely missing the point.


  • There's one missing:

     One_hundred_and_eeeeeiigghhty !



  • @mott555 said:

    I really wonder what thought process leads programmers to think that's a good idea.

    Paid by the line?  or perhaps magic number policy as mentioned previously


  • @boh said:

    I don't see the obligatory

    static final String TWENTYFIVE_PERCENT = "12.5%"; // VAT percentage changed

    Also, why would you have commas in the string up to 10k, but not for 100k and above?

    My guess would be that the developer doesn't know how to convert an int to a string; particularly a formatted string.  The higher numbers are probably too long for the text field he's outputting to when the commas are included.

    I'd say I work with this guy, but the devs at my current company aren't capable of this level of sophistication.



  •  Depending on the precision required:

     

    static final String PI = "355 / 113";

    or

    static final String PI = "4 * arctan(1)";

     



  • @da Doctah said:

    Filed under: <FONT color=#698d73>Or in Indiana: static final String PI = "3"</FONT>

    static final String PI = "dee-lishus";



  •  For more bonus points, it's the only one that isn't a string.



  • static final int SIX = 6;

    static final int NINE = 7; // HACK - fudged to get the correct answer to the ultimate question

    static final int ANSWER_TO_ULTIMATE_QUESTION = SIX * NINE; // took seven and a half million years to get this one right, so don't mess with it!

    static final int NUMERIC_SYSTEM_BASE = 10; // just in case the French take over the world and switch us all to base 12

    static final int SIXTY_NINE = SIX *  NUMERIC_SYSTEM_BASE + NINE; // TODO - figure out why socket interface program is losing some bits when instantiating a child process

    static final int CHINESE_RESTAURANT_FUDGE = 2; // XXX - they don't really serve fudge at chinese restaurants, need to deprecate this!

    static final int GENERAL_TSOS_CHICKEN = SIXTY_NINE + CHINESE_RESTAURANT_FUDGE; // the fudge factor makes the joke about the restaurateur and his wife work, otherwise we get moo goo gai pan which makes no sense in this context


Log in to reply