• the beholder (unregistered)

    public static final int INTEGER_FRIST = 1;

  • Kelderek (unregistered)

    Good to see that INTEGER_ONE and INTEGER_FIVE are defined twice. (Just in case)

  • COMMENT_FRIST (unregistered)

    COMMENT_FRIST = 1;

  • The Nerve (unregistered)

    Yeah, but why don't they do

        public static final Long LONG_VALUE_TEN = Long.valueOf(INTEGER_VALUE_TEN);
        public static final Long LONG_VALUE_ZEARO = Long.valueOf(INTEGER_VALUE_ZEARO);
    
        public static final BigDecimal BIGDECIMAL_ZERO = new BigDecimal(INTEGER_VALUE_ZEARO);
        public static final BigDecimal BIGDECIMAL_ONE = new BigDecimal(INTEGER_VALUE_ONE);
    
        public static final BigDecimal NEGATIVE_NUMBER_ONE = new BigDecimal(-INTEGER_VALUE_ONE);
    

    Those comparator values are bogus, which is an example why it's ok to use integers sometimes.

  • thenextguy (unregistered)

    Auch, that hurts!

    I use magic numbers all the time, I just make sure there's a comment line next to it explaining them. e.g. malloc(17) // 4*4(int) + 1 (\0)

  • (cs)

    Now, if 1 turned up to be 9, I don't mind, I don't mind. If all the hippies cut off their hair, I don't care, I don't care. Dig, 'cause I got my own world to live through And I ain't gonna copy you.

  • The Nerve (unregistered)

    ONE == INTEGER_VALUE_ONE == PLUS_ONE == COMPARE_RESULT_ONE

    This is a perfect example of my new favorite design pattern: Cargo Cult.

  • COHERENCE_NOT_FOUND (unregistered)

    public static final float FLOAT_NOT_QUITE_PI = (LONG_VALUE_TEN + LENGTH_TWELVE) / (NINE_STRING + (COMPARE_RESULT_NEGATIVE * (PLUS_ONE + BIGDECIMAL_ONE) ));

    mmmmmm... FLOAT_NOT_QUITE_PI..

  • kerberos (unregistered)

    TRWTF: public static final String ONE_STRING = "9";

  • Matt Westwood (unregistered)

    If that;s Java it won't compile because of the repeated INTEGER_FIVE etc.

  • MatDey (unregistered)

    public static final String ONE_STRING = "9";

    lolz

  • (cs)

    This is for future compatibility. If Java will ever drop primitive types, this guy is prepared.

  • by (unregistered) in reply to kerberos
    kerberos:
    TRWTF: public static final String ONE_STRING = "9";

    Or maybe that's the beauty of the Constants class. If you want 5 to universally be 6, all you have to do is change a value in Constants!

  • by (unregistered) in reply to Matt Westwood
    Matt Westwood:
    If that;s Java it won't compile because of the repeated INTEGER_FIVE etc.

    Copy fail.

  • The Boss (unregistered)

    TRWTF is that this isn't in an XML file.

  • (cs) in reply to kerberos
    kerberos:
    TRWTF: public static final String ONE_STRING = "9";

    On the contrary, that shows the real flexibility of using constants. He was able to change ONE_STRING from "1" to "9" without having to dig through countless lines of source code.

  • (cs)
    Allen's colleague:
    public static final int COMPARE_RESULT_ZERO = 0 ; public static final int COMPARE_RESULT_ONE = 1 ; public static final int COMPARE_RESULT_NEGATIVE = -1 ;

    It seems likely these are being used somewhere in conjunction with a Comparable or Comparator. If that's the case there's another WTF here in assuming that compareTo() methods will return -1 for "less than" and +1 for "greater than". The only assumption you're allowed to make is that it returns a negative value for "less than" and a positive value for "greater than".

  • Anonymous (unregistered)

    Ahh, I see the WTF - what if they want a short or a float? Clearly this developer didn't go far enough with their implementation. Kudos for having constants specifically defined for comparison though - you just never know when they'll redefine the return values on comparator methods. Last time I updated my distribution I found that 17 was greater-than, 43 was less-than and -4 was equality. If only I'd had the foresight to use constant values instead of inline integers.

  • Larry (unregistered) in reply to Erasmus Darwin
    Erasmus Darwin:
    kerberos:
    TRWTF: public static final String ONE_STRING = "9";

    On the contrary, that shows the real flexibility of using constants. He was able to change ONE_STRING from "1" to "9" without having to dig through countless lines of source code.

    TRWTF is that Java has no way to change constant values dynamically.

  • Paula (unregistered)

    UPPERCASE_T_STRING + LOWERCASE_H_STRING + LOWERCASE_A_STRING + LOWERCASE_T_STRING + ONE_SPACE_STRING + LOWERCASE_I_STRING + LOWERCASE_S_STRING + ONE_SPACE_STRING + LOWERCASE_B_STRING + LOWERCASE_R_STRING + LOWERCASE_I_STRING + LOWERCASE_L_STRING + LOWERCASE_L_STRING + LOWERCASE_A_STRING + LOWERCASE_N_STRING + LOWERCASE_T_STRING + EXCLAMATION_MARK_STRING

  • (cs) in reply to thenextguy
    thenextguy:
    Auch, that hurts!

    I use magic numbers all the time, I just make sure there's a comment line next to it explaining them. e.g. malloc(17) // 44(int) + 1 (\0)

    Tsk. The 44(int) is presumably to indicate that you want 4 lots of 4-byte ints, right? But ints aren't 4 bytes (well, not necessarily, anyway: I once worked on a system where it would have been reasonable for CHAR_BITS == INT_BITS == 16, and plenty of systems a bit later with sizeof(int) == 2). Also, this is just duplicating the magicness in the comments: what is the first 4? And why not this?

    malloc( 4*sizeof(int)+1 ); // one int per season of year, 1 byte for trailer

    Now the comment explains what the magic number means.

  • the beholder (unregistered) in reply to Erasmus Darwin
    Erasmus Darwin:
    kerberos:
    TRWTF: public static final String ONE_STRING = "9";

    On the contrary, that shows the real flexibility of using constants. He was able to change ONE_STRING from "1" to "9" without having to dig through countless lines of source code.

    And he's also prepared to face possible issues if oracle ever decides to change the lenght of literals 1 or 10

  • Ethan Qix (unregistered)

    I think the last line was the funniest :

    public static final String ONE_STRING = "9";

  • argh (unregistered) in reply to the beholder
    the beholder:
    public static final int INTEGER_FRIST = 1;

    Don't u guys ever sleep? Perhaps a new joke next time at least? Is it too much to ask?

  • argh (unregistered) in reply to thenextguy
    thenextguy:
    Auch, that hurts!

    I use magic numbers all the time, I just make sure there's a comment line next to it explaining them. e.g. malloc(17) // 4*4(int) + 1 (\0)

    Why don't u write malloc(4 * sizeof(int) + 1) instead?

  • guest (unregistered)

    public static final Long LONG_VALUE_TEN = Long.valueOf(10); public static final Long LONG_VALUE_ZEARO = Long.valueOf(0);

    Guy apparently needs a spell check as well as a programming lesson

  • (cs) in reply to COHERENCE_NOT_FOUND
    COHERENCE_NOT_FOUND:
    public static final float FLOAT_NOT_QUITE_PI = (LONG_VALUE_TEN + LENGTH_TWELVE) / (NINE_STRING + (COMPARE_RESULT_NEGATIVE * (PLUS_ONE + BIGDECIMAL_ONE) ));

    mmmmmm... FLOAT_NOT_QUITE_PI..

    Note to spoil your joke or anything... but Java already has a double not quite pi constant: java.lang.Math.PI

    On another track, Java already has java.math.BigDecimal.ZERO and java.math.BigDecimal.ONE as well.

  • SR (unregistered)

    system.out.println(Constants.INTEGER_ZEARO + "th");

  • (cs)

    The Long and BigDecimal versions may make sense, if the java implementation is stupid enough to assume a constant number is an int and does a cast every time you assign a constant to a Long or BigDecimal.

  • pallen (unregistered)

    ALPHALET_L ALPHALET_O ALPHALET_L

    (I'm not spam!)

  • vtcodger (unregistered)

    TRWTF is that this isn't in an XML file

    Well, maybe. If 1 really equals 9 when XML is used, that might explain some problems I've been encountering.

  • wtf (unregistered) in reply to powerlord

    I see the problem.

    public static final int INTEGER_FOUR = 4;
    

    should have been

    public static final int INTEGER_FOUR = FOUR;
    

    That's it, right?

    Fixed.

  • Bill's kid (unregistered) in reply to argh
    argh:
    the beholder:
    public static final int INTEGER_FRIST = 1;

    Don't u guys ever sleep? Perhaps a new joke next time at least? Is it too much to ask?

    Apprently it is.

  • (cs) in reply to Steve The Cynic
    Steve The Cynic:
    thenextguy:
    Auch, that hurts!

    I use magic numbers all the time, I just make sure there's a comment line next to it explaining them. e.g. malloc(17) // 44(int) + 1 (\0)

    Tsk. The 44(int) is presumably to indicate that you want 4 lots of 4-byte ints, right? But ints aren't 4 bytes (well, not necessarily, anyway: I once worked on a system where it would have been reasonable for CHAR_BITS == INT_BITS == 16, and plenty of systems a bit later with sizeof(int) == 2). Also, this is just duplicating the magicness in the comments: what is the first 4? And why not this?

    malloc( 4*sizeof(int)+1 ); // one int per season of year, 1 byte for trailer

    Now the comment explains what the magic number means.

    Sigh. Sense of humour fail.

  • (cs)

    Way, way back in the yearo ZEARO...

  • OhHi (unregistered)

    isFileNotFound(TRWTF_ZEARO) = 1;

  • (cs) in reply to COMMENT_FRIST
    COMMENT_FRIST:
    COMMENT_FRIST = 1;

    Assertion fail!

  • Seol (unregistered) in reply to powerlord
    powerlord:
    COHERENCE_NOT_FOUND:
    public static final float FLOAT_NOT_QUITE_PI = (LONG_VALUE_TEN + LENGTH_TWELVE) / (NINE_STRING + (COMPARE_RESULT_NEGATIVE * (PLUS_ONE + BIGDECIMAL_ONE) ));

    mmmmmm... FLOAT_NOT_QUITE_PI..

    On another track, Java already has java.math.BigDecimal.ZERO and java.math.BigDecimal.ONE as well.
    Well, yeah, it's sensible to have static frequently-used values for immutable classes to save having to repeatedly create new objects.
  • JayC (unregistered)

    TRWTF is that this is actually an attempt to make Java Text-To-Speach friendly.

  • Billy The Squid (unregistered)

    There are times when people mock me for not being able to code in C, C++, or Java - I'm a PHP developer (NO, stop screaming, that which I write is non WTF worthy - I fix WTF level code more often than not, have a severe hatred of anyone who's looked at "PHP for dummies" and thought "Hey, I've heard of PHP").... I can understand C++, Java, have written in Assembler, RPG, and Cobol in the past (RPG Cobol itself is a WTF), yet I code in PHP for the fun of it.

    But this... This is evil. This code is wrong.

    This is beyond "magic numbers", and is just laughable. But better yet:

    I want to see the code when they have to change INTEGER_FOUR to have a value of 5 due to some management decision... I want to see STRING_FOUR have a value of "12".

    Think of it. And then laugh nervously with me.

  • SDR (unregistered)

    I have been quietly reading, enjoying, and sharing the wft for years. Today I am oddly compelled to comment. This one is literally melting my brain.

    OMFGWTF

  • Pretty Mad Developer (unregistered)

    This kind of code often comes from having the "magic constant" rule enabled in PMD, and a "zero PMD warning" policy. This proves, if anything, that blindly applied rules are almost always stupid.

    TRWTF is, what do you do if Oracle suddenly decides to change Java keywords? That's where C got it right with preprocessor directives:

    #define IF_KEYWORD if

    (captcha: "odio" -> "hatred" in spanish)

  • (cs) in reply to Billy The Squid
    Billy The Squid:
    I want to see the code when they have to change INTEGER_FOUR to have a value of 5 due to some management decision...
    Not 5, but 5.1 (i.e., a floating-point value). The level of confusion caused by that will be truly a matter to cheer the soul…of Satan and his minions.
  • NoAstronomer (unregistered) in reply to Larry
    Larry:

    TRWTF is that Java has no way to change constant values dynamically.

    LMAO That had me spraying coffee over my keyboard. Should be a featured comment.

    If only more languages had the ability to change constants on the fly. It's the way of the future.

  • LennyPain (unregistered) in reply to Paula
    Paula:
    UPPERCASE_T_STRING + LOWERCASE_H_STRING + LOWERCASE_A_STRING + LOWERCASE_T_STRING + ONE_SPACE_STRING + LOWERCASE_I_STRING + LOWERCASE_S_STRING + ONE_SPACE_STRING + LOWERCASE_B_STRING + LOWERCASE_R_STRING + LOWERCASE_I_STRING + LOWERCASE_L_STRING + LOWERCASE_L_STRING + LOWERCASE_A_STRING + LOWERCASE_N_STRING + LOWERCASE_T_STRING + EXCLAMATION_MARK_STRING

    "That is brillant!" ??? Error: Won't compile!

  • noobtuber (unregistered)

    public static final int FOUR = 4; public static final int INTEGER_FOUR = 4;

    ah now it makes sense

  • Matt Westwood (unregistered) in reply to Someone You Know
    Someone You Know:
    Allen's colleague:
    public static final int COMPARE_RESULT_ZERO = 0 ; public static final int COMPARE_RESULT_ONE = 1 ; public static final int COMPARE_RESULT_NEGATIVE = -1 ;

    It seems likely these are being used somewhere in conjunction with a Comparable or Comparator. If that's the case there's another WTF here in assuming that compareTo() methods will return -1 for "less than" and +1 for "greater than". The only assumption you're allowed to make is that it returns a negative value for "less than" and a positive value for "greater than".

    Oh wake up. You just check the return value as greater-than-or-equal to the ONE value and less-than-or-equal-to the "NEGATIVE" value. Cor I dunno, do I have to do ALL your design for you.

  • Matt Westwood (unregistered) in reply to SDR
    SDR:
    I have been quietly reading, enjoying, and sharing the wft for years. Today I am oddly compelled to comment. This one is literally melting my brain.

    OMFGWTF

    I sincerely hope your use of "literally" is literally incorrect.

  • (cs) in reply to JayC
    JayC:
    TRWTF is that this is actually an attempt to make Java Text-To-Speach friendly.

    I hope you're trying to be ironic...

  • Andreas (unregistered)

    What is so bad about magic numbers? There are definitely some areas, namely all numbers that are used in more than one place. But I have seen more code where the developer did too much, than I have seen code where magic numbers were a bad thing. Some examples? Replace boolean-parameters with named constants like: DO_THIS = true; DONT_DO_THIS = false; The method-call read: foo(DO_THIS) and foo(DONT_DO_THIS) This does not help reading! Or the guy converting all assertion-messages to string-constants! Did not help the reading either.

    Magic numbers are way overrated (I guess because it is so easy to check if someone uses them). You should use your brain instead of converting everything into a constant.

Leave a comment on “Avoiding Magic Constants”

Log In or post as a guest

Replying to comment #:

« Return to Article