Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > [Newbie Question] Magic Number's dangerous?

Reply
Thread Tools

[Newbie Question] Magic Number's dangerous?

 
 
Monique Y. Mudama
Guest
Posts: n/a
 
      11-26-2005
On 2005-11-25, Gordon Beaton penned:
> On Fri, 25 Nov 2005 09:31:37 -0600, Alan Krueger wrote:
>> But only if the number or string has the same semantic meaning. If
>> two numbers appear and happen to be the same value, but one could
>> change independently of the other, it doesn't make sense to
>> artificially tie them together.
>>
>> I know, that should fall under the common sense, but as in other
>> things, common sense isn't as common as it should be.

>
> I remember discovering the following during a code review:
>
> #define ONE 1
> #define TWO 2
> #define THREE 3
> [...]
>
> ...all the way to a hundred or so.
>
> I asked the programmer if he thought that any of these values might
> need to change one day (no), or if he could tell me what any of them
> meant (only by looking at the code where they were used).
>
> Nobody had ever explained to him the *purpose* of using symbolic
> constants, just that it was a bad idea to have the values themselves
> directly in the code.
>
> /gordon


Eek.

I had to work with code that had some brilliant lines:

#define PKT_TEN 10
#define PKT_FIFTEEN 15
#define PKT_TWNTYTWO 22
#define PKT_TWENTYTHREE 23

Yes, not only did the previous dev hard-code these numbers, he
actually spelled twenty differently in various packet names.

So then I got to go tracing through the code, trying to figure out
what the heck packet 15 did, anyway.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
 
 
 
Monique Y. Mudama
Guest
Posts: n/a
 
      11-26-2005
On 2005-11-25, http://www.velocityreviews.com/forums/(E-Mail Removed) penned:
> Ahh ok.
>
> Knew the concept however I never realised that it was known as Magic
> Numbers. Was someone in Sun smoking something at the time when they
> came up with that name I wonder?


I don't even remember the first time I heard the term Magic Number.
I'm sure it didn't have any connection to Java, though.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
 
 
 
Monique Y. Mudama
Guest
Posts: n/a
 
      11-26-2005
On 2005-11-25, (E-Mail Removed) penned:
> Hello, All
>
> There are many magic numbers located in our java source code.
> Someone pointed out that this is not good coding style. What
> dangerous appeared? used Magic Numbers
>
> Thanks


There are a few reasons.

One: at the moment you write the code, you may know very well that 3
represents the number of holes you expect to see in a piece of
notebook paper. But six months from now,

NUM_HOLES_IN_PAPER

is much clearer than

3.

If I've seen one thing in my career, it's that what is "obvious" today
is completely opaque in a few months.

Two: What if you have several classes that have to know how many holes
are in a piece of notebook paper? If you hard-code '3' into several
files, life will be hard when your paper source changes to 2-hole
paper. Also, in every place where '3' is used, you will have to think
about whether '3' represents the number of holes in paper, or whether
it actually represents the number of toner cartridges you had
available. Or whatever.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
Monique Y. Mudama
Guest
Posts: n/a
 
      11-26-2005
On 2005-11-26, Monique Y. Mudama penned:
>
> I had to work with code that had some brilliant lines:
>
> #define PKT_TEN 10
> #define PKT_FIFTEEN 15
> #define PKT_TWNTYTWO 22
> #define PKT_TWENTYTHREE 23
>
> Yes, not only did the previous dev hard-code these numbers, he
> actually spelled twenty differently in various packet names.
>
> So then I got to go tracing through the code, trying to figure out
> what the heck packet 15 did, anyway.


I forgot to mention the fun part: he and I ended up working on
different projects, but anyway, I got laid off, and he didn't.

My co-workers were livid. (He was infamous for writing awful,
unmaintainable code, so much so that there was a name for his awful
code derived from his last name.)

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-26-2005
On Fri, 25 Nov 2005 17:13:52 -0700, "Monique Y. Mudama"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>I don't even remember the first time I heard the term Magic Number.
>I'm sure it didn't have any connection to Java, though.


I first heard the term in the context of numbers you precalculated.
Today compilers can evaluate the corresponding expressions at compile
time.

There is a magic-number in the Class file format. 0xCAFEBABE. It is
just a signature at the head to identify class files.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Mike Gaab
Guest
Posts: n/a
 
      11-26-2005

"Monique Y. Mudama" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2005-11-25, Gordon Beaton penned:
>> On Fri, 25 Nov 2005 09:31:37 -0600, Alan Krueger wrote:
>>> But only if the number or string has the same semantic meaning. If
>>> two numbers appear and happen to be the same value, but one could
>>> change independently of the other, it doesn't make sense to
>>> artificially tie them together.
>>>
>>> I know, that should fall under the common sense, but as in other
>>> things, common sense isn't as common as it should be.

>>
>> I remember discovering the following during a code review:
>>
>> #define ONE 1
>> #define TWO 2
>> #define THREE 3
>> [...]
>>
>> ...all the way to a hundred or so.
>>
>> I asked the programmer if he thought that any of these values might
>> need to change one day (no), or if he could tell me what any of them
>> meant (only by looking at the code where they were used).
>>
>> Nobody had ever explained to him the *purpose* of using symbolic
>> constants, just that it was a bad idea to have the values themselves
>> directly in the code.
>>
>> /gordon

>
> Eek.
>
> I had to work with code that had some brilliant lines:
>
> #define PKT_TEN 10
> #define PKT_FIFTEEN 15
> #define PKT_TWNTYTWO 22
> #define PKT_TWENTYTHREE 23
>
> Yes, not only did the previous dev hard-code these numbers, he
> actually spelled twenty differently in various packet names.
>
> So then I got to go tracing through the code, trying to figure out
> what the heck packet 15 did, anyway.


Hi Monique,

(I love that name. )

Just curious, what would have been a better naming scheme?
Sometimes it is really hard to come up with one.

Mike



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Alan Krueger
Guest
Posts: n/a
 
      11-26-2005
Roedy Green wrote:
> There is a magic-number in the Class file format. 0xCAFEBABE. It is
> just a signature at the head to identify class files.


A Unix version of Rational's Purify I once used filled uninitialized
memory with 0xDEADBEEF.
 
Reply With Quote
 
Alan Krueger
Guest
Posts: n/a
 
      11-26-2005
Roedy Green wrote:
> On Fri, 25 Nov 2005 09:31:37 -0600, Alan Krueger
> <(E-Mail Removed)> wrote, quoted or indirectly quoted someone
> who said :
>
>>I know, that should fall under the common sense, but as in other things,
>>common sense isn't as common as it should be.

>
> Applause! Did you make that up?


No, it's an observation. How many times have you heard people say
something should be common sense in the context of someone having not
used it?
 
Reply With Quote
 
Mike Gaab
Guest
Posts: n/a
 
      11-26-2005

"Thomas Hawtin" <(E-Mail Removed)> wrote in message
news:43875a95$0$1493$(E-Mail Removed)...
> zero wrote:
>> "Mike Gaab" <(E-Mail Removed)> wrote in
>> news:(E-Mail Removed):
>>>public static final short Sun= 0;
>>>public static final short Mon = 1;
>>>public static final short Tues = 2;
>>>public static final short Wed = 3;
>>>public static final short Thurs= 4;
>>>public static final short Fri = 5;
>>>public static final short Sat = 6;

>
>> Since 1.5 it might be better to use enums.
>>
>> enum DayOfWeek { Sun, Mon, Tues, ... Not sure how thos affects
>> performance though, and for something simple this may be overkill.

>
> Overkill? Seems to reduce quantity and improves clarity of the code.
>


From what I have seen from other posters especially Lasses' post,
I totally disagree. Enums are very OO but, IMO, far from more readable
than a simple named constant. My preference would be to use named
constants but I am not saying there isn't a useful application of Java
enums.

Mike



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
josh.s17@gmail.com
Guest
Posts: n/a
 
      11-26-2005
I have noticied a trend recently that programmers don't treat Magic
numbers in JSP code in the same way as magic numbers in straight java.
In my opinion magic numbers anywhere are bad and should be avioded.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
magic / counter-magic? (detect loading file?) Giles Bowkett Ruby 9 12-17-2007 05:42 AM
Network Magic 2.0 Gene Hora Wireless Networking 10 01-02-2006 11:03 PM
Wrapper on magic line? Nilsson Mats Perl 0 12-09-2003 02:48 PM
"Magic" password to the IBM 8265 switch - HELP ! gaucho Cisco 2 10-17-2003 08:29 PM



Advertisments