Comment On Magic Number Superstition

John was maintaining a client management system written a few years before his time and noticed that the original coder had a strange belief about magic numbers. He believe that any number less than three was not "magic" and, therefore, perfectly acceptable to hard code. But numbers three and above ... hard-coding those was just a flat-out bad practice. Everyone knows that you should use strings for those ... [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Magic Number Superstition

2005-10-20 14:02 • by -L
Alex Papadimoulis:


caseMgr.setAssetId(result.getResults().elementAt(0).toString());
caseMgr.setTypName(result.getResults().elementAt(1).toString());
caseMgr.setValCode(result.getResults().elementAt(2).toString());
caseMgr.setValDesc(result.getResults().elementAt(Integer.parseInt("3")).toString());


Is there a 10 character limit to the method names in Java?



As for the rest, I cannot think of any excuses doing it this way. A real WTF.

Re: Magic Number Superstition

2005-10-20 14:03 • by rogthefrog
47461 in reply to 47460
Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.

Re: Magic Number Superstition

2005-10-20 14:05 • by quamaretto
Now, wait a second, haven't you heard of Trinary Java? To use decimal, he would have had to prepend each decimal number with 0d, and that would have looked silly. Lay off of him.

Re: Magic Number Superstition

2005-10-20 14:10 • by Canuck
This is probably one of those situations where the client complained
that the original code was too efficient, so the developper went back
and tried to slow it down.  I think I spoke to him, and he was
saying that if the client still wasn't satisified, is next step was
going to be something like:


run(aPhoneNumber, "substring", new Object[]{new
Integer(Integer.parseInt("3"))}, new Integer(Integer.parseInt("3"))],
new Class[]{int.class, int.class});


...

Re: Magic Number Superstition

2005-10-20 14:12 • by travisowens
47466 in reply to 47465

Smart guy to use 3 as the cutoff because if he went to 4, then he would have to hardcode Pi


(This post is  approved)

Re: Magic Number Superstition

2005-10-20 14:18 • by frosty
47467 in reply to 47466
It's like the stories I hear that go like this:



A non-software engineers will write a 500 line function.  Later,
he is told the function is not necessary.  His response is to
spend another 40 lines undoing what the previous 500 lines did.

Re: Magic Number Superstition

2005-10-20 14:18 • by Joshie
I was going to post a real comment about this, but I just can't stop laughing:
But numbers three and above ... hard-coding those was just a flat-out bad practice. Everyone knows that you should use strings for those ...

Re: Magic Number Superstition

2005-10-20 14:18 • by Lews
47469 in reply to 47461
rogthefrog:
Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.


Double check the parenthesis again. He's calling toString() on elementAt(Integer.parseInt("3"))

Re: Magic Number Superstition

2005-10-20 14:20 • by nekomimi
47470 in reply to 47466
Object-oriented programming at its finest.



No, really.

Re: Magic Number Superstition

2005-10-20 14:30 • by lalala
This happens when you have to follow brain dead coding standards and
design rules. I bet that poor guy had been hit over his head with a 200
page style guide, because he once hard coded a number greater 3. This
was his little revenge: Code in full compliance with the rules, so no
quality clown could reject the code.

Re: Magic Number Superstition

2005-10-20 14:35 • by kipthegreat
47479 in reply to 47476
Anonymous:
This happens when you have to follow brain dead coding standards and
design rules. I bet that poor guy had been hit over his head with a 200
page style guide, because he once hard coded a number greater 3. This
was his little revenge: Code in full compliance with the rules, so no
quality clown could reject the code.




I think (hope) that this is what actually happened... 

Re: Magic Number Superstition

2005-10-20 14:42 • by Howard M. Lewis Ship
He was probably on a project that used Checkstyle to look for magic
numbers and used parseInt() as a stupid end-run around the Checkstyle
rules.  He should have defined int constants for what each element
meant (ASSET_ID_INDEX = 0, TYPE_NAME_INDEX = 1), etc.

Re: Magic Number Superstition

2005-10-20 14:53 • by frosty
47482 in reply to 47480
Anonymous:
He should have defined int constants for what each element
meant (ASSET_ID_INDEX = 0, TYPE_NAME_INDEX = 1), etc.




This is one coding practice often see ignored.  It's a bother if
it's only ints that aren't defined constants, but it's a mess if it's
strings that show up in 20 places in code.

Re: Magic Number Superstition

2005-10-20 14:54 • by Anonymous
If I close my eyes and count to Integer.parseInt("10")).toString(), maybe it will go away.  ;^)

Re: Magic Number Superstition

2005-10-20 14:56 • by Grimoire
47484 in reply to 47480
Anonymous:
He was probably on a project that used Checkstyle to look for magic
numbers and used parseInt() as a stupid end-run around the Checkstyle
rules.  He should have defined int constants for what each element
meant (ASSET_ID_INDEX = 0, TYPE_NAME_INDEX = 1), etc.


Or even constants like ZERO, ONE, TWO, THREE, FOUR, etc.  For full marks, make sure that ZERO is not actually 0...

Re: Magic Number Superstition

2005-10-20 15:11 • by cdoom
What, no try/catch block around the parseInt call!

Re: Magic Number Superstition

2005-10-20 15:15 • by Eduardo Habkost
47488 in reply to 47476
I guess we've found the author of the code. 8)

Really, in an environment like "lalala" described, I would do the same.

Re: Magic Number Superstition

2005-10-20 15:16 • by kipthegreat
47489 in reply to 47480
Anonymous:
He was probably on a project that used Checkstyle to look for magic
numbers and used parseInt() as a stupid end-run around the Checkstyle
rules.  He should have defined int constants for what each element
meant (ASSET_ID_INDEX = 0, TYPE_NAME_INDEX = 1), etc.




Maybe he could have used (1+1+1) for 3.....

Re: Magic Number Superstition

2005-10-20 15:16 • by Eduardo Habkost
47491 in reply to 47488
Anonymous:
I guess we've found the author of the code. 8)

Really, in an environment like "lalala" described, I would do the same.


Ugh. Just in case: this was supposed to be a reply to "lalala" comment.

Re: Magic Number Superstition

2005-10-20 15:28 • by emptyset
47492 in reply to 47467

Anonymous:
It's like the stories I hear that go like this:

A non-software engineers will write a 500 line function.  Later, he is told the function is not necessary.  His response is to spend another 40 lines undoing what the previous 500 lines did.


ain't that the truth.  *zing*

Re: Magic Number Superstition

2005-10-20 15:30 • by emptyset
47495 in reply to 47484

Grimoire:
ZERO is not actually 0...


for full kroner, #define Z 0

Re: Magic Number Superstition

2005-10-20 15:33 • by brazzy
47496 in reply to 47461
rogthefrog:
Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.




Actually, it isn't the same in ANY spec-conforming Java implementation.
The former will create a different object each time it is executed,
while the latter is taken from the class's constant pool. Or, in code:



"3" == "3"

is true, while

"3" == Integer.parseInt("3").toString()

is false.



Of course, it hardly ever makes sense to deliberately create
non-identical strings with the same content, and if you really have to,
that's what the String(String) constructor is for.

Re: Magic Number Superstition

2005-10-20 15:35 • by RevMike
47498 in reply to 47461
rogthefrog:
Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.




Well, of course.  He is trying to account for localization.

Re: Magic Number Superstition

2005-10-20 15:35 • by masklinn
47500 in reply to 47495
emptyset:

Grimoire:
ZERO is not actually 0...


for full kroner, #define Z 0


Nope, #define Z 7

Re: Magic Number Superstition

2005-10-20 15:36 • by kipthegreat
47501 in reply to 47496
brazzy:
rogthefrog:
Maybe
Integer.parseInt("3").toString() isn't the same as "3" in his
implementation of Java.




Actually, it isn't the same in ANY spec-conforming Java implementation.
The former will create a different object each time it is executed,
while the latter is taken from the class's constant pool. Or, in code:



"3" == "3"

is true, while

"3" == Integer.parseInt("3").toString()

is false.



Of course, it hardly ever makes sense to deliberately create
non-identical strings with the same content, and if you really have to,
that's what the String(String) constructor is for.




I think when he said "isn't the same", he meant it like most people
would take it, that it represents the same set of characters (i.e.
.equals()), which would be true.

"3".equals("3")    is true

"3".equals(Integer.parseInt("3").toString())   is also true

Re: Magic Number Superstition

2005-10-20 15:37 • by RevMike
47503 in reply to 47496
brazzy:
rogthefrog:
Maybe
Integer.parseInt("3").toString() isn't the same as "3" in his
implementation of Java.




Actually, it isn't the same in ANY spec-conforming Java implementation.
The former will create a different object each time it is executed,
while the latter is taken from the class's constant pool. Or, in code:



"3" == "3"

is true, while

"3" == Integer.parseInt("3").toString()

is false.



Of course, it hardly ever makes sense to deliberately create
non-identical strings with the same content, and if you really have to,
that's what the String(String) constructor is for.




However...


"3" == Integer.parseInt("3").toString().intern()


is true

Re: Magic Number Superstition

2005-10-20 15:37 • by kipthegreat
47504 in reply to 47501
kipthegreat:
brazzy:
rogthefrog:
Maybe
Integer.parseInt("3").toString() isn't the same as "3" in his
implementation of Java.




Actually, it isn't the same in ANY spec-conforming Java implementation.
The former will create a different object each time it is executed,
while the latter is taken from the class's constant pool. Or, in code:



"3" == "3"

is true, while

"3" == Integer.parseInt("3").toString()

is false.



Of course, it hardly ever makes sense to deliberately create
non-identical strings with the same content, and if you really have to,
that's what the String(String) constructor is for.




I think when he said "isn't the same", he meant it like most people
would take it, that it represents the same set of characters (i.e.
.equals()), which would be true.

"3".equals("3")    is true

"3".equals(Integer.parseInt("3").toString())   is also true




Of course it should be noted that the WTF code never actually did   Integer.parseInt("3").toString()

Re: Magic Number Superstition

2005-10-20 15:41 • by kipthegreat
47507 in reply to 47504
kipthegreat:
kipthegreat:
brazzy:
rogthefrog:
Maybe
Integer.parseInt("3").toString() isn't the same as "3" in his
implementation of Java.




Actually, it isn't the same in ANY spec-conforming Java implementation.
The former will create a different object each time it is executed,
while the latter is taken from the class's constant pool. Or, in code:



"3" == "3"

is true, while

"3" == Integer.parseInt("3").toString()

is false.



Of course, it hardly ever makes sense to deliberately create
non-identical strings with the same content, and if you really have to,
that's what the String(String) constructor is for.




I think when he said "isn't the same", he meant it like most people
would take it, that it represents the same set of characters (i.e.
.equals()), which would be true.

"3".equals("3")    is true

"3".equals(Integer.parseInt("3").toString())   is also true




Of course it should be noted that the WTF code never actually did   Integer.parseInt("3").toString()




Now that I think about it,  
Integer.parseInt("3").toString()   would never
compile...  Integer.parseInt("3") is an int, which is primitive,
so .toString() couldn't compile....

Re: Magic Number Superstition

2005-10-20 15:42 • by rogthefrog
47508 in reply to 47469
Anonymous:
rogthefrog:
Maybe
Integer.parseInt("3").toString() isn't the same as "3" in his
implementation of Java.


Double check the parenthesis again. He's calling toString() on elementAt(Integer.parseInt("3"))




Oh right, my mistake. It all makes sense now.


 * rogthefrog goes off to change all his code to reflect this superior design pattern

Re: Magic Number Superstition

2005-10-20 16:15 • by My Name
Why don't you just make ten louder and make ten be the top number and make that a little louder?

Re: Magic Number Superstition

2005-10-20 16:17 • by kipthegreat
47513 in reply to 47512
Anonymous:
Why don't you just make ten louder and make ten be the top number and make that a little louder?




is that a spinal tap reference?  (i've never seen it...)

Re: Magic Number Superstition

2005-10-20 16:29 • by Xaprb
47514 in reply to 47512
But these go to eleven!



Seriously, I suspect what's really happened here is someone did a
find/replace for some stuff and hit "Replace All" without meaning
to.  I have personally done that.  I accidentally did
s/""/String.Empty/g in some .NET code once and never found the ensuing
havoc until much later.

Re: Magic Number Superstition

2005-10-20 16:34 • by NonDev
47516 in reply to 47514

Anonymous:
But these go to eleven!

I accidentally did s/""/String.Empty/g in some .NET code once and never found the ensuing havoc until much later.


Wow! Coding through search&replace - gotta love that :)


NonDev

Re: Magic Number Superstition

2005-10-20 17:43 • by OneFactor
47524 in reply to 47483

Anonymous:
If I close my eyes and count to Integer.parseInt("10")).toString(), maybe it will go away.  ;^)


But keep in mind that counting to Integer.parseInt("010") is not the same as counting to 010. [H]

Re: Magic Number Superstition

2005-10-20 17:47 • by OneFactor
47526 in reply to 47524

for (int i = 0; isTrue(i < Integer.parseInt("3")); i++) {
   switch i:
      case 0: System.out.print("W"); break;
      case 1: System.out.print("T"); break;
      case 2: System.out.print("F"); break;
}

Re: Magic Number Superstition

2005-10-20 18:57 • by Tony Morris
47531 in reply to 47496
This is not true. The J2SE API Specification does not guarantee that an
object is created for this method, nor does it guarantee that an object
is not created. The argument that is passed to the method is taken from
the constant pool (see VM Spec 4) and an "object is created" (it is
indeed created - JLS 3.10.5 iirc) in the same way that one is created
for all other String literals - simply, both code samples create an
object at class load time (the class containing that constant pool),
not one or the other. You may have been confusing that Integer.parseInt
returns "something else" (an int) that is not the instance representing
the passed String literal itself.



Here is a related topic to the one I believe was attempting to be highlighted.

http://jqa.tmorris.net/GetQAndA.action?qids=68&showAnswers=true



Oh, and if you want to argue, I implement the spec. as my day job :) but I'm always up for a constructive debate.

Re: Magic Number Superstition

2005-10-20 19:02 • by Tony Morris
47532 in reply to 47531
I thought I pressed the "quote" button.

I was referring to:




Actually, it isn't the same in ANY spec-conforming Java implementation.
The former will create a different object each time it is executed,
while the latter is taken from the class's constant pool. Or, in code:



"3" == "3"

is true, while

"3" == Integer.parseInt("3").toString()

is false.



Of course, it hardly ever makes sense to deliberately create
non-identical strings with the same content, and if you really have to,
that's what the String(String) constructor is for.




Re: Magic Number Superstition

2005-10-20 19:58 • by David P. Murphy
47534 in reply to 47531
Anonymous:
This is not true. The J2SE API Specification does not guarantee that an
object is created for this method, nor does it guarantee that an object
is not created. The argument that is passed to the method is taken from
the constant pool (see VM Spec 4) and an "object is created" (it is
indeed created - JLS 3.10.5 iirc) in the same way that one is created
for all other String literals - simply, both code samples create an
object at class load time (the class containing that constant pool),
not one or the other. You may have been confusing that Integer.parseInt
returns "something else" (an int) that is not the instance representing
the passed String literal itself.



Here is a related topic to the one I believe was attempting to be highlighted.

http://jqa.tmorris.net/GetQAndA.action?qids=68&showAnswers=true



Oh, and if you want to argue, I implement the spec. as my day job :) but I'm always up for a constructive debate.




I have nothing constructive to say.  I simply want to note that this proves that it is one fscked-up language.



ok

dpm

Re: Magic Number Superstition

2005-10-20 21:07 • by Knuta
47536 in reply to 47507
kipthegreat:


Now that I think about it,  
Integer.parseInt("3").toString()   would never
compile...  Integer.parseInt("3") is an int, which is primitive,
so .toString() couldn't compile....




Yes, this should clearly be solved with a

(new Integer(Integer.parseInt("3")).toString() ;)

Re: Magic Number Superstition

2005-10-20 21:48 • by SchoolhouseRock
I just think this programmer grew up listening to Schoolhouse Rock.  Because everyone who listened to that knows that "3 is a magic number."

Re: Magic Number Superstition

2005-10-21 00:36 • by OpBaI
47542 in reply to 47526
OneFactor:

for (int i = 0; isTrue(i < Integer.parseInt("3")); i++) {
   switch i:
      case 0: System.out.print("W"); break;
      case 1: System.out.print("T"); break;
      case 2: System.out.print("F"); break;
}



You will never understand.

int errors = 0;
for (int i = 0; isTrue(i < Integer.parseInt("3")); i++)
{
switch(i)
{
default:
if(i == 0)
{
System.out.print("W");
}
else if(i == 1)
{
System.out.print("T");
}
else if(i == 2)
{
System.out.print("F");
}
else
{
errors += 1;
}
}
}
if(isTrue(errors == Integer.parseInt("12"))
{
throw new SwitchCaseFailure("Invalid arguments.");
}

Re: Magic Number Superstition

2005-10-21 01:29 • by llxx

"Magic numbers"? This example looks a bit too artificial.


Regarding hard-coded values, what I do is just hard-code them in when they aren't likely to be changed and use defines when they are. Converting a string to an int? I hardly see the advantages of this scheme.


Style guidelines don't help much if taken to the extreme.

Re: Magic Number Superstition

2005-10-21 02:30 • by ray
Not to be funny or anything Alex, but that is the lamest WTF I've seen on this site. Running out of WTF's???

Re: Magic Number Superstition

2005-10-21 02:39 • by petvirus
47556 in reply to 47555
RAY, YOU ARE GAY

Re: Magic Number Superstition

2005-10-21 03:11 • by ray
47557 in reply to 47556
BLOW ME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Re: Magic Number Superstition

2005-10-21 04:02 • by NineSisters
47558 in reply to 47557
Come on. The poor code being written by 12 years old kids is not fun
any more. It's just the same kind of crap any inexperienced programmer
writes in his first learning year. :D

Re: Magic Number Superstition

2005-10-21 04:09 • by ray
47559 in reply to 47558
Agreed!

Re: Magic Number Superstition

2005-10-21 04:12 • by ray
47560 in reply to 47504
Thats why in java we use .equals() to compare the content of the object. ;)

Re: Magic Number Superstition

2005-10-21 04:17 • by ray
47561 in reply to 47496
brazzy:
rogthefrog:
Maybe
Integer.parseInt("3").toString() isn't the same as "3" in his
implementation of Java.




Actually, it isn't the same in ANY spec-conforming Java implementation.
The former will create a different object each time it is executed,
while the latter is taken from the class's constant pool. Or, in code:



"3" == "3"

is true, while

"3" == Integer.parseInt("3").toString()

is false.



Of course, it hardly ever makes sense to deliberately create
non-identical strings with the same content, and if you really have to,
that's what the String(String) constructor is for.




Forgot the quote button. :)



Anyway as I said, this is the reason we have the .equals() funtion in
java, as it allows us to compare the contents of an object as opposed
to "= =" that will compare the objects themselves to see if they are in
fact the same object.

Re: Magic Number Superstition

2005-10-21 04:19 • by ammoQ
47562 in reply to 47551
llxx:

"Magic numbers"? This example looks a bit too artificial.


Regarding hard-coded values, what I do is just hard-code them in
when they aren't likely to be changed and use defines when they are.
Converting a string to an int? I hardly see the advantages of this
scheme.


Style guidelines don't help much if taken to the extreme.





While I cannot confirm this particular example, I'm know programmers
who have comparable habits. For example, they say "global variables are
bad" and the build incredible complex constructs to imitate the
semantics of global variables (but of course it's not better at all,
since it is the semantic that makes global variables bad)

« PrevPage 1 | Page 2 | Page 3Next »

Add Comment