r/shittyprogramming • • Oct 06 '15

How to write more easily readable boolean functions

Post image
234 Upvotes

42 comments sorted by

18

u/HansVader Oct 06 '15

This is actually posted quite frequently here. Should improve the quality of shit posting.

30

u/SaraphL Oct 06 '15

I'll try to deliver more polished turd next time.

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

u/SaraphL Oct 06 '15

That parser must've felt retarded.

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

u/Sohcahtoa82 Oct 07 '15

Still not Enterprise enough. Needs a BooleanFactoryFactoryFactory.

10

u/draconk Oct 06 '15

what about null? why nobody thinks of the null?!

15

u/Neebat Oct 06 '15

I've been thinking of null for the last 30 minutes.

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

u/Rangsk Oct 07 '15

You really ought to add an exception to the end in case both conditions fail.

0

u/[deleted] Oct 06 '15

[deleted]

2

u/[deleted] Oct 06 '15

is this js?

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

u/zman0900 Oct 07 '15

Then you have Java's Boolean which might be null.

16

u/[deleted] 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

u/GranPC Oct 06 '15

And Lua

5

u/Spim Oct 06 '15

Ehhhh, PHP is C-like

39

u/Cutlesnap Oct 06 '15

I find that offensive

21

u/Spim Oct 06 '15

I understand, PHP offends me on a daily basis

1

u/[deleted] Oct 06 '15

Well, C is type safe, except if you ignore the type system.

7

u/hunyeti Oct 06 '15

correction: C can be (kind of) typesafe, most of the times, sometimes.

22

u/[deleted] Oct 06 '15

Then just do a return !!isEmpty();

1

u/stone_henge Oct 06 '15

That is shitty programming

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!

Here's an imgur mirror


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

u/xallaboutx Oct 06 '15

return isEmpty();

15

u/SaraphL Oct 06 '15

Give this guy a trophy.

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

u/Rockytriton Oct 09 '15
if (!isEmpty() == false) { 
    return true;    
} else {
    return isEmpty();
}

1

u/ryancohen Oct 06 '15

So, !!Equal technically means that it is?

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 False

1

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.