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?

 
 
seungwoo.yu@gmail.com
Guest
Posts: n/a
 
      11-25-2005
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

 
Reply With Quote
 
 
 
 
clarke.jonathan@gmail.com
Guest
Posts: n/a
 
      11-25-2005
hmm....Let me be the first to say "huh"?

perhaps your refering to the mythical magic number beast that lives
within the MS java VM. Its sole mission is to eat decent java code?

 
Reply With Quote
 
 
 
 
Mike Gaab
Guest
Posts: n/a
 
      11-25-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>


IMO, the worst thing is that it makes your code hard to read, and
therefore it is hard to understand. Numbers standing alone communicate
little to nothing except for a maybe a 0 or 1. Then it follows that your
code will be hard to maintain and can be error prone.

That's my two cents worth.
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
 
Mike Gaab
Guest
Posts: n/a
 
      11-25-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> hmm....Let me be the first to say "huh"?
>
> perhaps your refering to the mythical magic number beast that lives
> within the MS java VM. Its sole mission is to eat decent java code?
>


A magic number is simply a number in your source.
For example, let's say we were tracking days of a week.

short dayOfWeek = 4;

4 is a magic number. It really has no meaning, does it mean it is the 4th
day
of the week or the 5th day of the week? We don't know by just looking at
it. Surely we could find out by hunting through other source code but that
is
a waste of time. So we could do the following. [brute force but you
get the idea]

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;
....
short dayOfWeek = Thur;

Now this makes the code easy to read and understand which makes your
code easier to maintain.

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
 
clarke.jonathan@gmail.com
Guest
Posts: n/a
 
      11-25-2005
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?

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-25-2005
On 24 Nov 2005 17:53:56 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote, quoted or
indirectly quoted someone who said :

>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


The problem is in maintenance. If someone changes them they may
change literals in not enough or too many places. With named
constants there is one place to fix and it is clearly labelled what
the constant means. In context in code it is not as clear.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      11-25-2005
On 24 Nov 2005 21:18:41 -0800, (E-Mail Removed) wrote:
> 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?


It's a common term in computer programming and it predates Sun.

http://en.wikipedia.org/wiki/Magic_n...programming%29

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
grubbyfans
Guest
Posts: n/a
 
      11-25-2005
though it takes place to store static final variables it's useful to
maintain the source code and modify

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      11-25-2005
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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      11-25-2005
(E-Mail Removed) wrote:
>
> 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


It is often the case that programmers get over draconian when it comes
to magic numbers. With a new hammer, everything becomes a nail.

If you need to allocate a buffer, it's better to write:

byte[] buff = new byte[8192];

(and use buff.length rather than repeating the number) than to write:

/**
* Size of buffer private to {@link #xyzFunction}.
*/
private static final SOME_BUFFER_LENGTH = 8192;

.... screens later ...

byte[] buff = new byte[SOME_BUFFER_LENGTH];

As a rule of thumb, if a number *or string* appears twice, always give
it a constant (or preferably eliminate subsequent appearances). For
other cases, use common sense.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
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