Re:What is a magic number, and why is it bad?
by arethuza on Wednesday August 28, 2013 @12:16PM
(#6290938)
I've seen a project that defined string constants like:
public static String HTTP = "http";
public static String COLON = ":";
public static String SLASH = "/";
with code that looked like: url = HTTP + COLON + SLASH + SLASH + ....
[NB I am NOT joking]
Re:What is a magic number, and why is it bad?
by tzs on Wednesday August 28, 2013 @01:51PM
(#6291530)
It can be fun with code like that to see if you can imagine situations where you could argue with a straight face that the code is OK.
public static String HTTP = "http";
An argument for this might be that it allows you to easily change to https by changing that one line, although then the name of the string would be misleading. public static String COLON = ":";
public static String SLASH = "/";
These are harder. Maybe you don't want ":" and "/" to appear literally because for some reason you grep for ":" and "/" a lot looking for instances that are syntactically significant in your language and you got tired of hitting those characters in URL strings.All of these potential excuses fall, though, to the objection that all three of those could have been rolled into a single string, with a sensible name like URL_COMMON_PREFIX:
public static String URL_COMMON_PREFIX = "http://";
Re:What is a magic number, and why is it bad?
by marcelocamanho on Wednesday August 28, 2013 @12:53PM
(#6291164)
This is 'the daily wtf' level stuff, but I believe it to be true.
Re:What is a magic number, and why is it bad?
by acheron on Wednesday August 28, 2013 @12:33PM
(#6291045)
I'm glad you are kidding about this, but thedailywtf.com is full of examples of people who weren't.
Re:What is a magic number, and why is it bad?
by pivnicek on Wednesday August 28, 2013 @11:46AM
(#6290711)
What really is the difference you are achieving? Are you going to redefine zero?
Re:What is a magic number, and why is it bad?
by simias on Wednesday August 28, 2013 @11:49AM
(#6290732)
That reminded me of that fun FORTRAN piece of trivia:
http://everything2.com/title/Changing+the+value+of+5+in+FORT...
"""A candidate for the title of "Strangest Thing in Computer Science" would be the fact that FORTRAN lets you change the value of what appear to be unalterable numbers. Plato would roll over in his grave.
I have heard rumors that later FORTRAN compilers had this "capability" designed out. But the venerable FORTRAN IV and FORTRAN 77 allowed you to do it"""
public final static int ZERO = 0; public final static int ONE = 1; public final static int TWO = 2; public final static int THREE = 3; public final static int FOUR = 4; ....
(jk)