• (cs)

    Bahahaha. I wonder if he ever realised how pointless it was. He could've maybe given it a bit more of a descriptive name, like HOW_MANY_PEOPLE_WILL_EVER_BE_INVOLVED_IN_MY_SEX_LIFE. He truly has yet to figure out the purpose of naming constants.

  • (cs)

    rofl at the code, not the previous joke.  Good one.

  • (cs)

    Reminded me of this snippet from Code Complete:

    "When naming constants, name the abstract entity the constant represents rather than the number the constant refers to.  FIVE is a bad name for a constant (regardless of whether the value it represents is 5.0).  CYCLES_NEEDED is a good name.  CYCLES_NEEDED can equal 5.0 or 6.0.  FIVE = 6.0 would be ridiculous.  By the same token, BAKERS_DOZEN is a poor constant name; DONUTS_MAX is a good constant name."

  • (cs)

    I agree with seizethedave-  ONE is a bad choice of names.  I would have gone with VALID_DB_STATE or something.  It's not bad to use constants for return values ect.

  • (unregistered) in reply to xTMFWahoo

    Exactly the kind of insight I would expect from a Wahoo.

    On a lighter note, I'd just like to point out that maybe we're wrong to criticise this code without more context.  Perhaps ONE is an acronym for a state that just happens to be encoded as the number 1.  It could stand for "Object Normally Entered", or any number of perfectly acceptable phrases.

  • (unregistered)

    I think the author of the code was doing a good job at being a defensive programmer.  If the value of 1 ever changed, he would only need to fix the code in one place.  Excellent foresight!

  • (unregistered) in reply to

    That’s no excuse, is ONE actually meant something, it should have been disambiguated ("spelled out" for you VB guys... take it easy, just joking!).

  • (unregistered)

    Sorry for the oh-so-lame joke. Acronym or not, it really shouldn't be named that. Like stated, should be like VALID_DB_STATE or DB_OPEN. Something worth looking at.

  • (unregistered)

    Yep, my first boss was a fan of defining constants like ONE and ZERO. I believe her reasoning was along the lines of: "Well, if people see a '1' or a '0' in code, they might think it's a magic number; but if they see 'ONE' or 'ZERO' they know that's what the coder really meant."

    Riiiight.  I could publish my own WTF based on some of the other programming pearls this person tried to teach me...


  • (unregistered)

    Word?

  • (unregistered)

    MIT Kerberos Version 4 (from the early 90's) had a global constant "ONE" with a value of 1, which eventually became:

    extern const int krbONE;
    #define         HOST_BYTE_ORDER (* (char *) &krbONE)
    

    to fix the namespace pollution (and from there, went away altogether as HOST_BYTE_ORDER became a configuration test - the last two releases of Kerberos v4 had a lot of code-level improvements...) The trick was that you could tell the byte order, if the 1 ended up in the first byte it was one way, if it didn't, it was the other way, and if you had a PDP 11/23 with "middle endian" support in your C (longs composed the "other" way from the bytes in the short halves) we laughed at you.

    So oddly, in this case, ONE really was a symbolic name - it just wasn't a very good one :-)

    _Mark_

  • (unregistered)

    <FONT style="BACKGROUND-COLOR: #efefef">Well, this Perl module says ONE => 'one' so I'm not sure who to believe. If two programs say they're Jesus, one of them must be wrong.</FONT>

    http://search.cpan.org/src/ABW/XML-Schema-0.07/lib/XML/Schema/Constants.pm

     

  • (unregistered)

    This is awesome!  When we finally ditch that arabic system and revert to Roman Numerals, this guy will be ready !

  • (unregistered)

    Of course, there are situations where ONE as a constant might make sense:

    Enum PersonalPronouns
        ONE = 1
        I
        WE
        I_AND_I
    End Enum

  • (unregistered) in reply to
    :
    Of course, there are situations where ONE as a constant might make sense:

    Enum PersonalPronouns
        ONE = 1
        I
        WE
        I_AND_I
    End Enum



    That won't work unless you add:
    Const ROYAL_WE = PersonalPronouns.I
  • (unregistered)

    Despite himself, this guy managed to do some good - when somebody competant comes along to actually replace the magic numbers, grepping for "ONE" will be a lot more informative than grepping for "1".

    (It's JoeNotCharles, but I can't be arsed to find the login button right now - why isn't there one right on the reply form?)

  • (cs) in reply to

    I think there is a logon button on the Reply page. Should be the one in the top-right. Do you like VB a lot, btw? [6]

    Actually, if someone is going to replace the magic numbers, he could get confused when he finds ONEHUNDRED or ONETHOUSAND too. [:P]

  • (cs) in reply to
    :
    This is awesome!  When we finally ditch that arabic system and revert to Roman Numerals, this guy will be ready !
    Yes! I simply love inputing "FiftySixMillionThreeHundredAndSeventyEightThousandFourHundredAndTwentySixDollars".

    Sure beats typing out the digits. Intellisense for that would be nice.....
  • (unregistered)

    Well, this isn't really a magic number, but a clever co-worker of mine once thought he could fake a stylesheet in a cold fusion application using the following:

    application.font.black = "black";
    application.font.white = "white";

    and then littering the html pages with:

    <font color="<cfoutput>application.font.black</cfoutput>">

    WTF. I think he sells insurance these days...

  • (unregistered)

    That's a bit like naming yourself anonymous so that no-one can identify you.
    A Poster.

  • The ONE and only (unregistered) in reply to

    In which case, if should be

    #define OBJECT_NORMALLY_ENTERED (1)

    and then there would be not "perhaps".

    Always code as if you might have to maintain it.

Leave a comment on “Avoiding Magic Numbers”

Log In or post as a guest

Replying to comment #:

« Return to Article