r/shittyprogramming • u/SaraphL • Oct 06 '15
How to write more easily readable boolean functions
9
u/Shockwave_ Oct 06 '15
I shit you not, I interned at a company and someone there found the line of code
Boolean.parseBoolean("true");6
60
u/SantaCruzDad Oct 06 '15
To make it more robust it should really be:
if (isEmpty() == true && isEmpty() != false)
return true;
else if (isEmpty() == false && isEmpty() != true)
return false;38
u/very_mechanical Oct 06 '15
Ready for Enterprise.
31
u/compdog Oct 06 '15
nah, Enterprise would be:
if (isEmpty() == VALUE_TRUE && isEmpty() != VALUE_FALSE) return VALUE_TRUE; else if (isEmpty() == VALUE_FALSE && isEmpty() != VALUE_TRUE) return VALUE_FALSE;14
10
4
u/kuilin Oct 06 '15
But then it might not return a value...
6
u/SantaCruzDad Oct 06 '15
If both tests fail then there is a logical inconsistency in the space-time continuum and the only option left is to initialise a self-destruct sequence, at which point there is not much point returning anything.
3
u/kuilin Oct 06 '15
Or isEmpty could return null or like a string or something weird.
2
u/RealModeX86 #define FALSE poop Oct 06 '15
Wouldn't a string evaluate to true? And wouldn't many languages treat NULL as false?
3
0
36
u/sjdaws Oct 06 '15
Just because it returns true doesn't mean it will return false. I've seen some pretty spectacular software in my life, might return an array of items or something when it's not empty.
33
u/euclio Oct 06 '15
Static typing is your friend.
6
u/Neebat Oct 06 '15
But as a friend, it's kind of a bummer, always being all inflexible and demanding to know exactly what you're up to before you're allowed to have any fun at all.
4
16
Oct 06 '15
This looks C based so I doubt it.
Type safe master race
12
u/pimp-bangin Oct 06 '15
This is valid JavaScript
22
u/tangerinelion Oct 06 '15
I think it's also valid PHP, C, C++, C#, Java, D and maybe Perl (are the braces required or optional?).
7
5
1
22
1
10
u/dongas420 Oct 06 '15
You should change those return values to TRUE and FALSE (all-caps!) to make it extra clear that your function returns boolean values. Don't forget to add #define TRUE ! true and #define FALSE ! false to the top of your code—the exclamation points after TRUE and FALSE will make your intentions known to the preprocessor as well, loud and clear.
7
u/PuushToImgurBot Oct 06 '15
I see you posted a Puu.sh Link!
I am a bot. this was generated automatically. Wonder why I'm here? Workplaces and Schools Block Puu.Sh Am I broken? Message my boss here.
1
2
u/immibis Oct 08 '15
if((isEmpty() == true) == true)
return true;
if((isEmpty() == false) == true)
return false;
if((isEmpty() == true) == false)
return false;
if((isEmpty() == false) == false)
return true;2
1
1
u/tajjet Oct 19 '15
what a dumb code it should be
if (isEmpty != null):
if (isEmpty != null && isEmpty == true && isEmpty != false):
return True
elif (isEmpty != null && isEmpty != true && isEmpty == false):
return False1
u/karanfilov Oct 06 '15
If you have the -buddhist compiler flag on it may return both true and false so code is legit to keep it in line with boolean logic.
18
u/HansVader Oct 06 '15
This is actually posted quite frequently here. Should improve the quality of shit posting.