/r/programminghorror
Share strange or straight-up awful code.
RULES:
All posts MUST show terrible code. There are no exceptions.
No Editor Themes - If it's just your editor that looks bad, it doesn't belong here.
No Advertisement Code. This is generally written by people in marketing who only know what "code" looks like from other ads. It's not real code, so it doesn't belong.
No Student Code. Yes, they're learning, but bad code is part of the process.
No Generated Code. If it's minified JS, generated XML, or what have you, we don't want it here. Yes, the YouTube homepage has an extra right-angle bracket. We know.
No Asking for Help. Go to r/learnprogramming. What are you doing here?
No Spamming/Advertising. We don't care about your shitty Youtube video or new crypto that will "change the world".
Be Nice. No hate speech of any kind is allowed, as well as generally being a jerk. Talk about the code, not eachother.
No Direct Contact of the Mods. Please use the modmail, we don't want to be contacted directly.
Please direct stories without code to /r/talesfromtechsupport, and programming questions to /r/learnprogramming
Programming Horror is where developers come together to revel in the idiocy of our peers.
This subreddit is meant for sharing funny programming related stories and strange or straight-up awful code.
For the sake of not being mauled by rabid lawyer bears, please make sure to anonymize your stories - changing the names of people and companies.
For code examples, indent all your lines with 4 spaces to make it more readable:
foo = 'bar'
Sister subreddits
/r/programminghorror
I'm sure everyone has seen joke code at some point in their career of an exhaustive if/else block checking if an int equals every possible even value for an isEven() function. Now, obviously, in C/C++ the best way to determine if a value is even doesn't even involve calling a function: just !(val&1). However, I thought it would be fun if we try to take on an algorithmic approach to writing bad isEven functions in a thread. Here are 3 to start:
This takes an int in flips a bool every time it decrements. When the int is 0, the bool will be returned:
bool isEven(int val) {
bool isEven = true;
while (val != 0) {
isEven = !isEven;
val--;
}
return isEven;
}
Here's another fun one one. This relies on the integer overflow and continuously increments the value by two until it equals either 0 or 1:
bool isEven(int val) {
while(1) {
if (val == 0) return true;
if (val == 1) return false;
val += 2;
}
}
Recursion is always fun. This returns true if the value is equal to 0, or returns the inverse of the value minus one if its not:
bool isEven(int val) {
return val == 0 ? true : !isEven(val - 1);
}
So how about it? What are your guy's bad isEven()?
I just found this screenshot in my google drive. I remember PROUDLY sending this to all my friends via a GOOGLE DOC thinking I was comedy legend
I was like 12 or smth don't judge
Found this code when investigating a bug and yes it was used in several locations.