- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
...are you mad? If many developers agree on this, I am going to have to become an artist.
A constant exists so it can be used in many places but defined and changed in just one. What are you going to change FOUR to? 5?
Constants are there to keep code DRY, not to give cute (wrong) names to things.
Admin
Forgot NULL_TERMINATION_CHARACTER.
Admin
why not malloc(num_items * sizeof(data_struct))? Or, better, just use calloc(...) It's going to be a constant when it compiles anyway, and it's more or less self-commenting.
The only place I find I need magic numbers is in my header files that describe binary structures.
Admin
it's only a problem of nomenclature! public static final int MAYBE_FOUR = 5; OR public static final int MY_FOUR = 5; AND we're ready to go!
captcha: similis. Almost an ugly disease.
Admin
This is not a WTF.
On embedded systems with no filesystem...
Admin
I think that most people already get this, but if they did, then this wouldn't keep cropping up so often.
Ok:
Slower, but more readable:
In case I forgot what 9.8 is or can't figure it out based on context:
or
Completely stupid:
Admin
I can hear Jimi!
Admin
Really? I always thought that those constants where EXACTLY equal to pi...
Admin
Admin
Admin
This gave me killer instincts.
Also, the captcha word for this comment was "genitus".
Admin
When you've calmed down you might want to read this. Does it make sense now?
Admin
Note the (somewhat misplaced) ellipsis. The original code included:
Admin
ONE_STRING to rule them all, ONE_STRING to find them ONE_STRING to bring them all and in the compiler bind them
Admin
Admin
FTW!
Oh wait.... sorry
#define FOR_THE_WIN_EXCLAMATIONPOINT_ABBREVIATION_STRING "For The Win!"
Admin
Not necessarily changed - the value of Math.PI [1] is never going to change, but it's still sensible to make it a constant so that (1) developers don't have to remember the value; and (2) they can't screw up by making a typo (OK, other than making it Math.PHI or something..).
[1] unless someone got it wrong originally...
Admin
Exactly.
You should use a constant definition, instead of the literal constant, if:
Otherwise, just use the constant, and if necessary, document what you're doing with a comment.
To use the example from the story, you wouldn't want to define a constant FOUR just so you could divide a value into quarters. If, however, you expect that the number of slices might one day change, then you define
#define NUMBER_OF_SLICES 4
x = y / NUMBER_OF_SLICES;
This is surely not news to any programmer, but it's surprising how many people just don't seem to get it.
Admin
Or, maybe you're running your program in Indiana.
Admin
Seems fine to me.
Admin
hahahaha, pwnd
Admin
hahahaha, pwnd
(whoops on the previous post)
Admin
Perhaps you have forgotten the most important constant.... INEGER for when an integer just won't do.
Captcha: Validus -- Lesser known brother of Spartacus.
Admin
nice, I can't tell if you're joking
Admin
Bah. I occasionally (rarely) use magic numbers in some of my code. For example, we have some numbered data sources (data suppliers), and while their numbers can't change, their names sometimes do, so translating from a name to a number using a table won't work.
If data source 17 is so-and-so, I'll put a 17 in the code, and comment who that supplier is.
And why are the negative numbers big integers?
Admin
I was replying a bit hasty. Didn't want to type that much :)
Admin
If I used to much comment, my comment quota for the day would run out and I'll have no more left for my actual code :(
Admin
Admin
I can't believe I looked at this long enough to realize that you spelled "brilliant" wrong. Nice...
Admin
Fixed.
Admin
TRWTF is that Java has no datatype for an infinite-precision number.
Admin
What happens when compareTo() returns an int between 0 and -1? It's important to consider edge cases like this.
Admin
You're mean. Schniffle.
(not to mention dumb as a bag of rocks, if you really think you're the only person reading this who understands the limitations of floating point precision or the impossibility of a precise representation of pi)
Admin
Actually, I think the troll is on the other foot.
Admin
Look man, you can listen to Jimi but you can't hear him.
Admin
As an aside, it was actually pretty smart to do this sort of thing in Ye Olde 8-bit Atari BASIC. Numeric literals consumed six bytes in the code, but variable references only took two bytes, so you could save a lot of space in large programs by converting all commonly-used numbers to variables.
100 N1=1:N2=N1+N1:N3=N2+N1:N4=N3+N1:N5=N4+N1
Ahhh, memories...
Admin
TRWTF is that Java is around 15 years old, and you still are not able to define non-Integer ints.
Admin
output of this program on machines that support this:
4
WTF?!?
(On other machines, e.g. Sun's Solaris Fortran, it crashes when writing to A in FRED() because the 7 is stored in a read-only section. It is still passed by reference, though.)
Admin
Admin
My eyes are burning. Is that the magic happening?
Admin
Haha, these could get bad.
Admin
I still love using the sizeof operator. Someday, your 64-bit integer program might move to a 128-bit machine.
int arr = malloc(4sizeof(int));
I have no idea why you put a NULL terminator on a non-string array.
Admin
I actually wrote something like that, it was:
const int NUMBER_OF_FINGERS = 10;
This was my response to the request to remove all magic constants from the code, part of which converted numbers from binary to base 10. The QA guy insited to change all nubmers to some meaningfull constants, decribing the reason and purpose of their use. After a moment of reflection I realized that was the only possible name for the constant.
Others included:
const int BEGIN_OF_THE_ARRAY = 0;
and
const int NUMBER_YOU_HAVE_TO_SUBTRACT_FROM_SIZE_OF_ARRAY_TO_GET_LAST_ELEMENT = 1;
Admin
Weirdly, the superposition of two integers is still an integer.
Admin
Black Magic. Or maybe Chemical Warfare.
Admin
FTFY
Admin
Dead arm! Humerus fail.
Admin
I guess we have a new running joke, but Bert Glanstron isn't very funny or creative. At least the Frists (at least the proper frists) do show some creativity.
OBTW.. I apologize if it's hard to read my comment, I have cold, and I fear that my typing is somewhat nisl sounding.
Admin
No, that's probably the refresh rate on your monitor. Try increasing it.
Admin
Not often, he doesn't.