Results 1 to 11 of 11

Thread: Magic Numbers?!

  1. #1
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default Magic Numbers?!

    hey all.
    i lost .5 marks in my assignmenta t uni for "using magic numbers, which you shoudln't"

    i have no idea what these are. is this a java term?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It's quite a general term: Magic number (programming) - Wikipedia, the free encyclopedia (You probably offended against 2: Unnamed numerical constants)

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    int[] values = {1,2,3,4,5};
    for(int index = 0; index < 5; index++) {
        System.out.println(values[index] / 10);
    }
    Here, 5 and 10 are considered magic numbers. Why are they bad? What happens if the length of the array changes? Suddenly the loop breaks. What if the value 10 is scattered 1000 times across a 1000000 line program? You would have to hunt down and change every instance instead of using a variable that can be changed once.
    Java Code:
    final int DIV = 10;
    int[] values = {1,2,3,4,5};
    for(int index = 0; index < values.length; index++) {
        System.out.println(values[index] / DIV);
    }

  4. #4
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    i probably did.
    the line above was

    if(args.length == 3){

    maybe that's what is wrong

    i did this because it MUST have 3 args, it also said i can assume they will enter the correct values :P

    what should've i done? if checking if the length is 3 ?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You should have used a variable and assigned the value 3 to it. In future if the requirements of the program changed to be more or less than 3 arguments then it is easier to find the variable declared at the top of your code than hunting through the body of the code to make the change. It is only a minor problem and hence you only lost a half a mark.

  6. #6
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You should have used a variable and assigned the value 3 to it. In future if the requirements of the program changed to be more or less than 3 arguments then it is easier to find the variable declared at the top of your code than hunting through the body of the code to make the change. It is only a minor problem and hence you only lost a half a mark.
    Thanks i get it now :)

    i know it's only a minor .5 of a mark but still i like ot know how i could've gotten better not jsut told 'you did it wrong, bye'

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Quote Originally Posted by maknib View Post
    i like ot know how i could've gotten better not jsut told 'you did it wrong, bye'
    This is the lovely part of having teachers who were not born to teach.

    I use magic numbers sometimes though, I mean, it kind of depends on the usage. I see no point in doing this:
    Java Code:
    int x = 5;
    if (someOtherVar == x) ...
    ...when I could do this:
    Java Code:
    if (someOtherCar == 5) ...

    Why would I waste the time to type it out, the memory to allocate it, and the storage to save it? If the variable has no other outside factors and is completely static in context, it needs no variable storage.

    [/rant]

  8. #8
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    exactly what i was thinking :D

    Rubble rubble rubble

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    For the very reasons already stated. What happens if the 5 gets changed to 6. It is much easier if you use a variable declared at the top of the class. Or even better use a Properties object.

  10. #10
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    i swing both ways here.

    i think it's easier just typing the 5 BUT at the same time i think it's better practice to use variables /properties

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The wikipedia article I linked to discusses "accepted usage" which "is subjective, and often depends on individual coding habits"

    They give these examples:

    Java Code:
    for (int i = [b]0[/b]; i < max; i = i + [b]1[/b]) //assuming i++ is not supported
    isEven = (x % [b]2[/b] == 0); // Is zero magic?
    circumference = [b]2[/b] * Math.PI * radius

    Regarding the last one, at least Math.PI is nonmagic! Things will be easy to change if ever pi were to change its value...

Similar Threads

  1. Magic squares help
    By mjpam in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 02:24 PM
  2. Need Help with Magic Square
    By easybe in forum New To Java
    Replies: 10
    Last Post: 04-23-2010, 09:39 PM
  3. Magic square
    By gandalf5166 in forum New To Java
    Replies: 20
    Last Post: 04-15-2010, 07:18 PM
  4. Magic Square!!!... :D
    By joms999 in forum New To Java
    Replies: 4
    Last Post: 02-25-2010, 07:55 AM
  5. Magic Eightball
    By sachmow in forum New To Java
    Replies: 1
    Last Post: 11-15-2009, 04:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •