Excerpt from code I recently saw:
#define EIGHT (0x08)
#define NINE (0x09)
#define TEN (0x10)
. . .
I hope I wasn't too subtle - the definition of TEN is what is ... interesting here.
Excerpt from code I recently saw:
#define EIGHT (0x08)
#define NINE (0x09)
#define TEN (0x10)
. . .
I hope I wasn't too subtle - the definition of TEN is what is ... interesting here.
Good thing they put parentheses around those literals!
edit:
Oh gosh, I didn't even realize the TEN issue... That's horrible.
edit:
Oh gosh, I didn't even realize the TEN issue... That's horrible.
That edit made my day :D - that is exactly why this is so scary.
an explanation for the more noobish of us? what's wrong with that definition? >_<;
an explanation for the more noobish of us? what's wrong with that definition? >_<;
0x10 in 16 in hex.
Could be binary coded decimal... and it makes outputting a text value easier as well ;)
Manic Miner on the ZX Spectrum didn't even store the game score or high score in a variable... it just used ASCII text on the screen (the screen area where the score was displayed was never cleared).
The algorithm only added to your score either 1 or 10 or 100 at a time IIRC. It just increased the ASCII value at the screen position by 1 and if that makes it > 9 it made the current digit 0 and added 1 to the next digit along (and looped if that was bumped to > 9 as well).
It then just copied the score to the high score location if the score was larger than the current high score (check was done with another ASCII comparison).
Could be binary coded decimal... [snip]
That sounds so convoluted it requires its own thread.
Unfortunately that wasn't the case here - this code, according to a colleague who originally fixed it - caused a fatal crash in the software.
Obvious, the coder has 8 fingers on each hand
... maybe the use of named constants is what was at fault here ...Obvious, the coder has 8 fingers on each hand
Eurgh. I have many horror stories of Octal gone awry.#define FOURTY_TWO (042)
#define FOURTY_TWO (042)
That's even worse!!!
That's even worse!!!
This used to happen to me when I was trying to align constants properly. Nowadays I just put spaces instead, been burned too often by this damn octal notation "feature" which I'm guessing nobody actually uses, except the odd raw socket hacker (if even). Permission bits are another one, but constants for those are already defined anyway.
at one point I made things more orthogonal (for my script language) by adding several number notations:
This used to happen to me when I was trying to align constants properly. Nowadays I just put spaces instead, been burned too often by this damn octal notation "feature" which I'm guessing nobody actually uses, except the odd raw socket hacker (if even). Permission bits are another one, but constants for those are already defined anyway.That's even worse!!!
Octal was big back in the day when C was being made, maybe even moreso than hexadecimal. These days nobody really uses it since it can't be aligned nicely to 8-bit (some computers back then had words with a bit count multiple of 3, so octal probably made a lot more of sense).
But they could have made it useful by adding pluses!
#define ONE +1
#define TWENTY +20
#define HUNDRED +100
// Here's a little example
#include <iostream>
int main()
{
int x = ONE;
std::cout << x << std::endl;
x = ONE HUNDRED;
std::cout << x << std::endl;
x = ONE HUNDRED TWENTY;
std::cout << x << std::endl;
x = ONE HUNDRED TWENTY ONE;
std::cout << x << std::endl;
}
Or even worse:
#define ONE +0x001
#define TWENTY +0x020
#define HUNDRED +0x100
....snip...
just...just....o god my eyes....
Still trying to figure out the reasoning behind that. Is that what happens when your code analysis tool complains about "magic numbers" and somebody just decides to "fix it"?
Because having one such tool complain about '0' being a "magic number" really made me question the worth of that tool and what the creators' code would look like.
But they could have made it useful by adding pluses!
#define ONE +1 #define TWENTY +20 #define HUNDRED +100 // Here's a little example #include <iostream> int main() { int x = ONE; std::cout << x << std::endl; x = ONE HUNDRED; std::cout << x << std::endl; x = ONE HUNDRED TWENTY; std::cout << x << std::endl; x = ONE HUNDRED TWENTY ONE; std::cout << x << std::endl; }Or even worse:
#define ONE +0x001 #define TWENTY +0x020 #define HUNDRED +0x100
Sadly this doesn't fully work:
TWO HUNDRED == 102
But don't worry - we can fix it! We just need to be "clever"...
#define AND +0
#define ONE +1
#define TWO +2
#define TWENTY +20
#define HUNDRED *100
// Here's a little example
#include <iostream>
int main()
{
int x = TWO HUNDRED AND TWENTY ONE;
std::cout << x << std::endl;
// prints 221
} This way you even get to write grammatically correct numbers. ![]()
I think we should pool our efforts and make a programming language where this kind of thing is sane.
I think we should pool our efforts and make a programming language where this kind of thing is sane.
This topic has been locked by a moderator. New replies are not allowed.
GameDev.net uses cookies to ensure you have the best experience on our platform. Learn more