Comment On Avoiding Magic Numbers

The term "Magic Numbers" generally refers to numeric constants embedded in code that serve an essential but often difficult to determine purpose. Avoiding code like "if (table.columns(8).row(12) = 772) ..." is generally regarded as a best practice. But I'm not sure Matt's colleague, however, grasped that concept too well ... [expand full text]
« PrevPage 1Next »

Re: Avoiding Magic Numbers

2004-12-30 12:22 • by Jacob K
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.

Re: Avoiding Magic Numbers

2004-12-30 12:59 • by codewulf
rofl at the code, not the previous joke.  Good one.

Re: Avoiding Magic Numbers

2004-12-30 13:33 • by seizethedave

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."

Re: Avoiding Magic Numbers

2004-12-30 13:40 • by xTMFWahoo
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.

Re: Avoiding Magic Numbers

2004-12-30 13:49 • by
27185 in reply to 27184
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.

Re: Avoiding Magic Numbers

2004-12-30 13:49 • by
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!

Re: Avoiding Magic Numbers

2004-12-30 14:40 • by
27187 in reply to 27185


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!).



Re: Avoiding Magic Numbers

2004-12-30 14:41 • by
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.

Re: Avoiding Magic Numbers

2004-12-30 15:18 • by
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...





Re: Avoiding Magic Numbers

2004-12-30 16:30 • by
Word?

Re: Avoiding Magic Numbers

2004-12-30 17:29 • by
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_

Re: Avoiding Magic Numbers

2004-12-30 19:33 • by

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.


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


 

Re: Avoiding Magic Numbers

2004-12-30 22:57 • by
This is awesome!  When we finally ditch that arabic system and revert to Roman Numerals, this guy will be ready !

Re: Avoiding Magic Numbers

2004-12-31 02:51 • by
Of course, there are situations where ONE as a constant might make sense:



Enum PersonalPronouns

    ONE = 1

    I

    WE

    I_AND_I

End Enum



Re: Avoiding Magic Numbers

2004-12-31 05:09 • by
27200 in reply to 27199
:
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

Re: Avoiding Magic Numbers

2005-01-01 14:12 • by

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?)

Re: Avoiding Magic Numbers

2005-01-03 08:16 • by Katja
27225 in reply to 27215
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]

Re: Avoiding Magic Numbers

2005-01-04 00:11 • by icelava
27268 in reply to 27198
:
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.....

Re: Avoiding Magic Numbers

2005-01-07 01:31 • by
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...

Re: Avoiding Magic Numbers

2005-01-13 05:12 • by
That's a bit like naming yourself anonymous so that no-one can identify you.

A Poster.

runescape gold

2011-05-06 01:20 • by runescape gold (unregistered)
« PrevPage 1Next »

Add Comment