• (cs)
    public static final UNIVERSAL_ZERO = 0; /* in case zero changes someday */
    public static final PROGRAM_ZERO   = 0; /* " " " */
    
    if (avalue - UNIVERSAL_ZERO > PROGRAM_ZERO) {
       /* It's positive */
    } else if (avalue - UNIVERSAL_ZERO < PROGRAM_ZERO) {
       /* It's negative */
    } else {
       /* It's zero */
    }
    
    
    

    ...avoided!

  • (cs) in reply to NoAstronomer
    NoAstronomer:
    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.

    If you are among those who believe that the future is XML and XSLT, then you are going to have to deal with variables that can't be changed after they're defined. Makes me wonder why the inventor(s) of XSLT called them variables in the first place.

  • Gary Olson (unregistered)

    This is a translation table. But without all the constraints of using a table; or all the constraints of translating the input to usable output.

  • anon (unregistered) in reply to Silfax
    Silfax:
    The most important apps in most companies are written in COBOL (mainly your payroll app).
    Our company uses Peoplesoft which sucks almost as bad as cobol.
  • jbik (unregistered)

    Guy must have worked with MISRA before...

    MISRAble.

  • St Mary's Ambulance for ByteShortIntLongDoubleFloatBrobdingnag (unregistered)

    public static boolean IRISH_GIRL = "Too gorgeous to be measured on the 1/0 scale";

    caecus - where the Demoplicans elect their candidates?

  • yername (unregistered) in reply to COHERENCE_NOT_FOUND
    COHERENCE_NOT_FOUND:
    mmmmmm... FLOAT_NOT_QUITE_PI..

    Yes, organic things that float are not quite pie.

  • (cs)

    Ah, but if you use FOUR you can redefine it later to be 5 so you don't need to subsequently change it everywhere in the code.

    Useful when you move into the world of 5 byte integers, 5 seasons, 5 legged animals, etc.

    And that automatically redefines that constant of 17 above to 26.

  • Captain Normal Form (unregistered) in reply to RogerC
    RogerC:
    NoAstronomer:
    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.

    If you are among those who believe that the future is XML and XSLT, then you are going to have to deal with variables that can't be changed after they're defined. Makes me wonder why the inventor(s) of XSLT called them variables in the first place.

    Because 'variables' don't vary, in a given scope. Get that elementary school "explanation" out of your mind as quickly as possible. They are merely names for (possibly) unknown things.

  • Paha Partasetä (unregistered) in reply to NoAstronomer

    Well, in ruby you can, you just need to handle the execption ;)

  • Patrick (unregistered)

    Oh, the stupid! It burns!

  • CAPTCHA: nobis (unregistered)

    200th!

  • Jasper (unregistered)

    I did this once as an April's Fools joke. We had a woman on the team who was doing code reviews, and we knew she was easy to get worked up about this.

    And she fell for it like a stone. It was very funny.

  • Ken (unregistered)

    My head literally exploded at the end.

  • psm321 (unregistered)

    I had a professor in college who allowed absolutely no "magic numbers". So if we were implementing a formula that required dividing by 2, and it wasn't some arbitrary thing that could change in the future, we would still have to name it something like DIVISION_FACTOR. I thought that was incredibly stupid and made code much harder to read.

  • Bosshog (unregistered)

    "Ninth!"

  • RBoy (unregistered) in reply to Andrew
    Andrew:
    RBoy:
    Bert Glanstron isn't very funny

    Good thing we have an authoritative opinion on that now. Now I realize my spontaneous chuckling every time I read a Bert Glanstron retread is due to something other than its funniness.

    Butthurt much?

    Ironically, my post was in reference to someone bitching at how unfunny the firsts were. So where was your response the post I was responding to?

    And your spontaneous chucking just proves that you are an idiot.

    "Durr.. Look, someone hit CTRL-V again, and changed a word!"

  • (cs) in reply to RBoy
    RBoy:
    Andrew:
    RBoy:
    Bert Glanstron isn't very funny

    Good thing we have an authoritative opinion on that now. Now I realize my spontaneous chuckling every time I read a Bert Glanstron retread is due to something other than its funniness.

    Butthurt much?

    Ironically, my post was in reference to someone bitching at how unfunny the firsts were. So where was your response the post I was responding to?

    And your spontaneous chucking just proves that you are an idiot.

    "Durr.. Look, someone hit CTRL-V again, and changed a word!"

    It's very similar to how 3 year olds like to watch the same DVD 100 times a week.

  • qbolec (unregistered)

    const int FIRST = 208;

    FIRST!

  • Pedant (unregistered) in reply to qbolec
    qbolec:
    Big Excel Sheet:
    Using,

    malloc(4 * sizeof(int) + sizeof(void *));

    might indicate a NULL terminator, whereas,

    malloc(4 * sizeof(int) + 1);

    is more likely to indicate a nul terminator.

    This is intriguing, how could you have \0 to be a terminator of an array of ints? How would the code that handles that look like? How would you store values larger than 2^24 and divisible by 256?

    I was just being a pedant, and picking up on the difference between NULL and nul. I can't think of any situation where you might want to do something like that.

  • Backward compatibility? (unregistered)

    This is an excellent idea for backward compatibility.

    public static final int I = 1; public static final int II = 2; public static final int III = 3; public static final int IV = 4; public static final int V = 5;

    ...and so forth. You could back port thousands of years old software with this. Brilliant.

  • PRS (unregistered)

    I used to work as a Software Engineer. I've seen stuff like that used in OS systems that live around for decades. When it comes time to upgrade, the compilers sometimes change the definition of what an Integers is, for example, 8 bits vs 16 bits. By defining these up front, they can redefine them quickly.

  • (cs) in reply to neminem
    neminem:
    hoodaticus:
    You need a better sarcasm detector dude. There is not a single non-sarcastic sentence in the entire article.
    the article:
    The problem with using "FOUR" is that, like "4", it's a Magic Constant.

    That sentence is not sarcastic, it's entirely true. Using FOUR is not really any better than using the integer 4.

    So the author believes that using the number four in a program is a problem. Yeah, your sarc detector is working just fine.

  • TDude58 (unregistered) in reply to frits

    Hendrix. Dig it!

  • huh (unregistered) in reply to Paula

    WTF?

    "That is brillant"

    Brilliant, my dear, brilliant.

  • Jasper (unregistered) in reply to huh
    huh:
    WTF?

    "That is brillant"

    Brilliant, my dear, brilliant.

    You are obviously a newbie at TheDailyWTF.

    Read this: http://thedailywtf.com/Articles/The_Brillant_Paula_Bean.aspx

  • DOT (unregistered)

    All the very intelligent folks stare at this and miss the most obvious WTF! - Data typing. Compilers have been smart enough to know the data context for years - and give a data type mismatch error, but other than a few exceptions (like PERL) the developer community holds on tightly to their fixed, pre-defined data types.

  • Former Airline Software Developer (unregistered)

    Once I worked with a developer who did this. She's been promoted. Twice. Admittedly she had a pleasing personality, and a couple of other great assets, but now she's in charge of evaluating actual programmers.

  • anonymous (unregistered) in reply to thenextguy

    You use a magic number (17) and then a magic formula that uses magic numbers to explain it. Dude, define the constant and be done with it!

  • web.config (unregistered)
    <appSettings> <add key="One" value="1"/> </appSetting>
  • Robin (unregistered) in reply to thenextguy

    A magic comment with 3 magic numbers :)

  • John (unregistered) in reply to thenextguy

    Oh HELL no.

    malloc(17) when sizeof(int) != 4? My sizeof(int) != sizeof(long) and I'm not sure what sizeof(short) is.

    The correct code here:

    malloc(4*sizeof(int) + 1); // 4 (int) + '\0'

    And even then, that's nonsensible: if you're allocating an array of integers bounded by a magic value like 0, myarry[n] will read 4 bytes.

  • Wyrdo (unregistered)

    dang. I think they should've kept Allen's colleague locked in the COBOL cave!

    Yeah, it's good to have a constant defined almost everywhere except perhaps when that number is part of a mathematical formula and is not many digits long. For example, if I had the C=2 Pi r formula in some code somewhere, I'd probably code it with a literal 2 because I'm really never going to change that unless I'm changing the whole formula anyway. Now, while I'd feel perfectly justified inserting the literal Pi approximation 3.14159264 there, but I'd probably just use a constant named Pi. Not because it's gonna change next Tuesday, but simply because 3.141592654 is kinda long.

    But the thing they missed here with the Constants thing is that the symbolic names are supposed to be meaningful.

    Furry cows moo and decompress.

  • Offf (unregistered)

    public static final int CHASSIT = 19

  • Louis (unregistered)

    A colleague of mine adds a prefix to his magic Constants, for better readability:

    In VB.net:

    private constTwentyone as Int32 = 21

    He thinks his program runs faster by adding const as prefix. I told him few times that const is a keyword and needs to be separated before his literal, but he just don't get it...

  • Xul (unregistered)

    final static int INFINITY = 3;

  • martin (unregistered) in reply to Paula
    ...+ 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

    "BRILLANT"?

  • Cherns (unregistered) in reply to Brent
    Brent:
    Jay:

    I recall when I was a kid, my math teacher explaining that pi cannot be exactly expressed by any fraction or finite-length string of decimals. He conluded by saying, "The only way to express the value of pi exactly is to say 'pi'."

    Which is not quite true -- you could give an algorithm for calculating it -- but you get the idea.

    This is why you need to use constants line ONE, TWO, etc. You never known when someone is going to redefine the integers to be multiples of arctan(1), just so that they can set PI equal to 4 and be exactly correct.

    I once had to maintain a FORTRAN program that took exactly the opposite approach: In calculating perspective views for the CalComp plotter (gives you an idea of the age of the application) there were a lot of trigonometric expressions on the order of X = cos(A) + sin(B) or something like that. In certain parts of the code the expressions were like X = cos(0.0) + sin(180.0). This preserved a nice symmetry with the rest of the code and gave the reader an idea of what was going on, but seemed wasteful to me at a time when cycles were expensive. Presumably this might be a good place to define constants like Cos0.

    And that reminds me of a calculator that was once being peddled by one of those Sharper-Image boutique companies when mathematical calculators were hot stuff. The ad explained that this was a complicated and expensive calculator, but contained a microprogramming flaw in which arctan(0) was returned as some false value, so the manufacturer had recalled the whole batch. The merchandiser was offering these units at a very attractive price, along with a paper sticker that said "arctan(0) is 0."

  • SPee (unregistered)

    Unfortunately I have seen this on my previous project. The code was checked and the "magic number" was high. So management assigned a programmer (who appearantly did not think of his own) to fix this.

    Other pieces of code in that class:

    public static final String empty = ""; public static final String EMPTY_STRING = ""; public static final String SPACE = ""; // <== no typo

    So imagine the rest of the code...

  • mk (unregistered)
    many developers agree that it's best to define a constant named "FOUR", and then use that constant instead.

    I've never met a developer that agreed to that. Maybe I just don't work with stupid people, or maybe this is a stupid strawman.

  • mk (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)

    You're an idiot. I hope no one pays you to code.

  • mk (unregistered) in reply to bertram
    bertram:
    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.

    It wasn't humor and it wasn't funny, moron.

  • mk (unregistered) in reply to Anonymous
    Anonymous:
    lesliev:
    many developers agree that it's best to define a constant named "FOUR"
    ...are you mad? If many developers agree on this, I am going to have to become an artist.

    A constant exists so it can be used in many places but defined and changed in just one. What are you going to change FOUR to? 5?

    Constants are there to keep code DRY, not to give cute (wrong) names to things.

    When you've calmed down you might want to read this. Does it make sense now?

    Unlike you, intelligent people grasp which parts of this are funny and which parts are stupid.

  • mk (unregistered) in reply to J. Madden
    J. Madden:
    Matt Westwood:
    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.

    Seems fine to me.

    That's because you're an ignoramus.

  • mk (unregistered) in reply to qbolec
    qbolec:
    I actually wrote something like that, it was:

    const int NUMBER_OF_FINGERS = 10;

    This was my response to the request to remove all magic constants from the code, part of which converted numbers from binary to base 10. The QA guy insited to change all nubmers to some meaningfull constants, decribing the reason and purpose of their use. After a moment of reflection I realized that was the only possible name for the constant.

    Others included:

    const int BEGIN_OF_THE_ARRAY = 0;

    and

    const int NUMBER_YOU_HAVE_TO_SUBTRACT_FROM_SIZE_OF_ARRAY_TO_GET_LAST_ELEMENT = 1;

    No, moron, it's not the only possible name ... BASE is the correct name.

  • mk (unregistered) in reply to Jay
    Jay:
    Kempeth:
    powerlord:
    Note to spoil your joke or anything... but Java already has a double not quite pi constant: java.lang.Math.PI

    Really? I always thought that those constants where EXACTLY equal to pi...

    I recall when I was a kid, my math teacher explaining that pi cannot be exactly expressed by any fraction or finite-length string of decimals. He conluded by saying, "The only way to express the value of pi exactly is to say 'pi'."

    Which is not quite true -- you could give an algorithm for calculating it -- but you get the idea.

    It's called an equation, imbecile.

  • mk (unregistered) in reply to frits
    frits:
    Nothing's New Under The Sun:
    This is hardly new...

    The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 at every appearance, the variable PI can be given that value with a DATA statement and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of pi change.

    -- Early FORTRAN manual for Xerox Computers -- http://en.wikiquote.org/wiki/Fortran

    Wow man, you just strawmanned into the void. Nobody claimed this to be new...just crap.

    What a stupid dick. Do you have any idea just how not new Early FORTRAN is? And the fact that this was a seminal version of the joke went flying over your tiny head.

  • mk (unregistered) in reply to hoodaticus
    hoodaticus:
    neminem:
    hoodaticus:
    You need a better sarcasm detector dude. There is not a single non-sarcastic sentence in the entire article.
    the article:
    The problem with using "FOUR" is that, like "4", it's a Magic Constant.

    That sentence is not sarcastic, it's entirely true. Using FOUR is not really any better than using the integer 4.

    So the author believes that using the number four in a program is a problem. Yeah, your sarc detector is working just fine.

    That quoted statement is clearly not sarcastic. You were called on your over-generalization, but couldn't admit to anything because you're an intellectually dishonest jackass.

Leave a comment on “Avoiding Magic Constants”

Log In or post as a guest

Replying to comment #:

« Return to Article