- 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
Admin
He's turned me into a newt!
Admin
Admin
Admin
A Coworker I knew this guy and the poster, The Bloviator lasted 2 years before getting laid off. He was also a level 4 engineer when working at the company and was quoted once saying “can look at my code, and whenever you see this and underscore, you know it’s a number !"
Admin
It involved two different versions of the Solaris native compiler. One would accept code like this:
FunkyCharacterBufferClass variable;
...
printf("%s\n", variable); // Note no & on the variable
(This is undefined behaviour, passing an non-POD object to the ellipsis part of an varargs function. The previous version of the compiler silently passed the address of the variable, exactly as if the & was there.)
The new version of the compiler sprayed warnings all over the place because this pattern was used extensively. The new version also passed the whole object by value, and the code ceased to work at all.
That whole place was a WTF in and of itself, and server-side JavaScript got one of my colleagues into the habit of formatting strings like this:
"Some text with a number at the end: " + integervariable
And he then brought this habit to C++, where it doesn't work quite so well.
Ah, well, I escaped from there.
Admin
Admin
Eh? Depending on their numeric values can be perfectly sensible. The primary use of an enum is to define a set of states, in which case the values should indeed not be relied upon, but there are plenty of cases where it does make sense. What if you're writing an app or library that reads in a binary file, and the relevant values are determined by some external spec rather than your own code? Certainly it's sensible then. And there's nothing wrong with using an anonymous enum solely as a convenient way to declare a series of numeric constants -- so long as they have meaningful names and are not just "FortyTwo" and whatnot.
Admin
durp, I'm dumb, I misread the comment I was replying to. And apparently the "delete" button doesn't actually do anything?
Admin
Admin
But you got better, right?
Admin
TRWTF is that anyone actually used octal. EVER.
Admin
Admin
Obviously, a coder's job is to know the language. But seriously, C, prepending a 0 changes the radix? WTF?
Admin
If compilers get integers wrong and this is the solution to that, I want to tell that compilers make floating point errors also -- and I really want to see his solution.
Admin
It shames me to admit it, but I have used numerical values of enums in production code. In fact, I had for loops that iterated over enums. I had to force my fingers to type the code in.
I made real sure that the enum declarations had big comments warning that both the order and value of the enums were critical, and to not change them unless you knew what you were doing.
BTW, is using enum numeric values MISRA compliant?
Admin
WRONG... 0x was added much later.
Remember, "C" was INTENDED to be "just one level above assembler". Given the computer capabilities at the time (specifically DEC PDP-11) this made tremendous sense. Minimal overhead, fine grained control, allowed the use of a more expressive format with very few negative impacts.
Admin
In Java, this is good means of eficiency since veriable get passed by reference and not meke copy.
Admin
Easy.
enum { zero, one, two, three, four, five, six, seven, eight, nine, ten, one_hundred = 100, one_thousand = 1000, ten_thousand = 10000,
};
float Pi = three + (float)one / ten + (float)four / one_hundred + (float)one / one_thousand + (float)six / ten_thousand;
Admin
Java already has the Integer class, if you were really worried about that. Creating an enum with spelled-out numbers does not help efficiency.
Admin
Admin
But think of the advantages! For example, if the value of "seven" changes, there's only one place to go to fix it!
Admin
I think a fun thing to do with code like this is to pick a number deep in the middle of such a list and change the value. Like:
If the list is long and/or the numbers have enough digits, this could be hard to spot. Then sit back and watch him try to find the error.
Admin
I think this story falls under the general heading, "Programmer Refused to Belive that his Code had a Bug and Insisted it Must Be a Compiler Error".
I recall a programmer I worked with who had a bug in her program so that a certain field printed as zero rather than the value it was supposed to have. So she tried twenty different ways to display the value of the field. They all showed zero. She insisted that there must be something wrong with the print function that it would not print the correct value, rather than even considering the possibility that she might never have set it.
Admin
"Integer" could be the result of autoboxing of an "int." But isn't that what you wanted (to pass the pointer)? In any case, the enum still doesn't help.
Admin
I had a Javascript version of the octal trick. Made a nicely formatted set of month options:
The Javascript validation code was fun. Especially since Javascript accepted 08 and 09. Can't recall what it interpreted it as...
Admin
Admin
I put this in an include file: ________________________________________________________=0;
Then I coded this in my main:
//______________________________________________________
// INITIALIZE DEFAULT UNIT VECTOR
// On second thought, we really // don't need the whole vector...
// ;
-Harrow.
Admin
I wouldn't say "unexpectedly"... But define your own immutable substitute for Integer if you want. What's that got to do with a the ridiculous enum in the article?
Admin
Admin
You even have that in todays "modern high level" languages, such as PHP: echo intval(42); // 42 echo intval(042); // 34 echo intval('042'); // Guess!
That last one is the one that bit me. Try to guess what it outputs, before looking it up.
Admin
I had to look it up. I won't spoil it, but it looks like there's an optional second argument which can be used to specify the base, when it might be ambiguous.
Anyway, there were plenty of characters that could have been used to denote octal. Why would they pick one which already had an excepted (different!) mathematical meaning at that position?
Admin
I actually wrote a file system for an embedded system. It divided the high memory into sectors, and used something similar to the FAT model. I wrote special crash-protected fopen, fclose, fread, fwrite, fgets routines for it. Main reason was to make it easy to implement I/O with values less than 8 bits in width to pack data without requiring a compression routine.
Admin
*Accepted. Ack...
Admin
#define SIX 1 + 5 #define NINE 8 + 1 printf("%d\n", SIX * NINE);
output: 42
Admin
Eventually you stop asking Why, and just grab a bottle and wait for its sweet, sweet embrace.
Admin
Admin
Hopefully this is NOT on a 16 bit system. -1 is not a good value for MaxValue.
Admin
Unfortunately, old chap, there's plenty of fools out there with a keyboard and a compiler at their disposal. You're obviously blind or affected by some kind of political correctness syndrome to point out the idiocy to an idiot.
Never be afraid to call a spade a spade.
Admin
Hum... You should try to run that code sometime. With all those integer divisions, it won't give you the result you expect.
Admin
At least on C99 it is has a defined behaviour.
Admin
first fool
Admin
Admin
Admin
Admin
But I've forgotten my PIN number!
Admin
Admin
Admin
Admin
Not for thirty years.