Results 1 to 11 of 11
Thread: Magic Numbers?!
- 04-12-2011, 02:31 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
- 04-12-2011, 02:58 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
It's quite a general term: Magic number (programming) - Wikipedia, the free encyclopedia (You probably offended against 2: Unnamed numerical constants)
- 04-12-2011, 03:09 AM #3
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:int[] values = {1,2,3,4,5}; for(int index = 0; index < 5; index++) { System.out.println(values[index] / 10); }
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); }
- 04-12-2011, 03:12 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
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 ?
- 04-12-2011, 03:27 AM #5
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.
- 04-12-2011, 03:34 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
- 04-12-2011, 06:07 AM #7
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:
...when I could do this:Java Code:int x = 5; if (someOtherVar == x) ...
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]
- 04-12-2011, 06:14 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
exactly what i was thinking :D
Rubble rubble rubble
- 04-12-2011, 06:18 AM #9
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.
- 04-12-2011, 06:24 AM #10
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
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
- 04-13-2011, 04:01 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
Magic squares help
By mjpam in forum New To JavaReplies: 3Last Post: 06-30-2010, 02:24 PM -
Need Help with Magic Square
By easybe in forum New To JavaReplies: 10Last Post: 04-23-2010, 09:39 PM -
Magic square
By gandalf5166 in forum New To JavaReplies: 20Last Post: 04-15-2010, 07:18 PM -
Magic Square!!!... :D
By joms999 in forum New To JavaReplies: 4Last Post: 02-25-2010, 07:55 AM -
Magic Eightball
By sachmow in forum New To JavaReplies: 1Last Post: 11-15-2009, 04:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks