[Newbie Question] Magic Number's dangerous?

Discussion in 'Java' started by seungwoo.yu@gmail.com, Nov 25, 2005.

  1. Guest

    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
     
    , Nov 25, 2005
    #1
    1. Advertising

  2. Guest

    Re: Magic Number's dangerous?

    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?
     
    , Nov 25, 2005
    #2
    1. Advertising

  3. Mike Gaab Guest

    <> wrote in message
    news:...
    > 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 =----
     
    Mike Gaab, Nov 25, 2005
    #3
  4. Mike Gaab Guest

    Re: Magic Number's dangerous?

    <> wrote in message
    news:...
    > 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 =----
     
    Mike Gaab, Nov 25, 2005
    #4
  5. Guest

    Re: Magic Number's dangerous?

    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?
     
    , Nov 25, 2005
    #5
  6. Roedy Green Guest

    On 24 Nov 2005 17:53:56 -0800, 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.
     
    Roedy Green, Nov 25, 2005
    #6
  7. Re: Magic Number's dangerous?

    On 24 Nov 2005 21:18:41 -0800, 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_number_(programming)

    /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
     
    Gordon Beaton, Nov 25, 2005
    #7
  8. grubbyfans Guest

    Re: Magic Number's dangerous?

    though it takes place to store static final variables it's useful to
    maintain the source code and modify
     
    grubbyfans, Nov 25, 2005
    #8
  9. 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
     
    Gordon Beaton, Nov 25, 2005
    #9
  10. 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/
     
    Thomas Hawtin, Nov 25, 2005
    #10
  11. Alan Krueger Guest

    Thomas Hawtin wrote:
    > 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];


    In my experience, this seems to be a carryover from coding in C/C++
    where you can't always tell the size of a buffer from the buffer itself.
    But this, then, fires the rule you mention next:

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


    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.
     
    Alan Krueger, Nov 25, 2005
    #11
  12. zero Guest

    Re: Magic Number's dangerous?

    "Mike Gaab" <> wrote in
    news::

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


    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.
     
    zero, Nov 25, 2005
    #12
  13. Re: Magic Number's dangerous?

    zero wrote:
    > "Mike Gaab" <> wrote in
    > news::
    >
    >
    >>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.

    Performance is unlikely to matter.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Nov 25, 2005
    #13
  14. zero Guest

    Re: Magic Number's dangerous?

    Thomas Hawtin <> wrote in
    news:43875a95$0$1493$:

    > zero wrote:
    >> "Mike Gaab" <> wrote in
    >> news::
    >>
    >>
    >>>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.
    >
    > Performance is unlikely to matter.
    >
    > Tom Hawtin


    Probably, but I haven't seen any tests (or done any myself) so I'd rather
    say that it *might* be an issue than just ignore it and possibly give
    someone bad information.

    --
    Beware the False Authority Syndrome
     
    zero, Nov 25, 2005
    #14
  15. Mike Gaab Guest

    Re: Magic Number's dangerous?

    "Thomas Hawtin" <> wrote in message
    news:43875a95$0$1493$...
    > zero wrote:
    >> "Mike Gaab" <> wrote in
    >> news::
    >>>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.
    >


    I am not up on recent Java features but I did look at the enum type prior to
    deciding to use named constants in the example above. Seemed to me like
    using Java enums confused the issue as they don't appear to be implemented
    as they are in many other languages. Not that implementing them differently
    is a bad thing , just not implemented as one would expect. For instance, how
    do I assign a value to each element? What are the default values? In the
    case above one could expect the values to be Sun=0, Mon=1 and so on but at a
    quick glance they did not appear to have those values. How do I get at each
    elements value to possibly re-assign its value?

    Since you are up to speed on enums, how about an example.

    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 =----
     
    Mike Gaab, Nov 25, 2005
    #15
  16. Roedy Green Guest

    On Fri, 25 Nov 2005 09:31:37 -0600, Alan Krueger
    <> 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?
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 25, 2005
    #16
  17. Roedy Green Guest

    On 25 Nov 2005 16:41:44 +0200, Gordon Beaton <> wrote,
    quoted or indirectly quoted someone who said :

    >I remember discovering the following during a code review:
    >
    > #define ONE 1
    > #define TWO 2
    > #define THREE 3
    > [...]


    LOL. I have added that one to
    http://mindprod.com/jgloss/unmainotherpeople.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 25, 2005
    #17
  18. Roedy Green Guest

    Re: Magic Number's dangerous?

    On Fri, 25 Nov 2005 12:44:09 -0700, "Mike Gaab" <>
    wrote, quoted or indirectly quoted someone who said :

    > For instance, how
    >do I assign a value to each element? What are the default values?


    see http://mindprod.com/jgloss/enum.html
    to answer your basic questions.

    Java numbers them automatically.

    If you want some other numbers or values, you can define a method and
    implement it for each enum constant, or pass it is the constructor and
    use a method common to all enum constants.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 25, 2005
    #18
  19. Re: Magic Number's dangerous?

    "Mike Gaab" <> writes:

    > I am not up on recent Java features but I did look at the enum type prior to
    > deciding to use named constants in the example above. Seemed to me like
    > using Java enums confused the issue as they don't appear to be implemented
    > as they are in many other languages. Not that implementing them differently
    > is a bad thing , just not implemented as one would expect. For instance, how
    > do I assign a value to each element?


    Like you assign values to other objects:
    ---
    enum EnumWithAValue {
    FOO(2),
    BAR(42),
    BAZ(87);

    public final int value;
    EnumWithAValue(int value) {
    this.value = value;
    }
    }

    // .... FOO.value == 2
    ---
    You can also assign more than one value to each instance.


    > What are the default values?


    There are none. The elements of an enumeration have nothing to
    distinguish themselves by except their object identity (well, they do
    have a name and an ordinal, but that's really implementation details).

    > In the case above one could expect the values to be Sun=0, Mon=1 and
    > so on but at a quick glance they did not appear to have those
    > values.


    Correct. Java enumerations define a type with a fixed number of
    values. It does so using a class with a fixed number of instances.

    C-like languages instead define a fixed number of integer constants.
    There is no type, so you cannot declare a variable to only take
    members of the enumeration as values. I.e., the Java way is type-safe,
    the C one is not.

    The Java enumeration implementation is taken from the way people were
    already implementing type-safe enums. It was explained in, e.g.,
    "Java Language Programming Guide" by Joshua Bloch (Item 21 IIRC).

    > How do I get at each elements value to possibly re-assign
    > its value?


    If you really want to be able to change it, make a settable value on
    each instance. Otherwise, just set value by the constructor.

    It really is just a class and instances of it. What makes it an enum
    is that there can only be a fixed number of instances. (A singleton
    is really just a trivial enumeration)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 25, 2005
    #19
  20. On 2005-11-25, Thomas Hawtin penned:
    > 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];



    Seems like you're dodging the issue here by giving a poor example.
    The fact is that you must have chosen 8192 for a reason, and that
    reason can be documented with a well-chosen variable name.

    --
    monique

    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
     
    Monique Y. Mudama, Nov 26, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lawrence

    bad magic number

    lawrence, Sep 20, 2003, in forum: Java
    Replies:
    5
    Views:
    844
    Roedy Green
    Oct 10, 2003
  2. Hendrik Maryns

    Magic number in Boolean

    Hendrik Maryns, Mar 10, 2006, in forum: Java
    Replies:
    15
    Views:
    1,125
    Hendrik Maryns
    Mar 13, 2006
  3. artie-effim

    MSVC .pch file magic number?

    artie-effim, Dec 1, 2004, in forum: C++
    Replies:
    1
    Views:
    462
    Victor Bazarov
    Dec 1, 2004
  4. Wes Gamble
    Replies:
    3
    Views:
    151
    Wes Gamble
    Mar 22, 2006
  5. Giles Bowkett
    Replies:
    9
    Views:
    426
    Giles Bowkett
    Dec 17, 2007
Loading...

Share This Page