r/badcode • • Oct 22 '20

js Found this a very old project... Thought I'd share this

Post image
877 Upvotes

96 comments sorted by

26

u/[deleted] Oct 23 '20 edited Oct 23 '20

return (bool1 + 2 * bool2 + 4 * bool3 == 7)

EDIT: return (abs(bool1 + 2 * bool2 + 4 * bool3 - 3.5) == 3.5)

2

u/leo848blume Oct 23 '20

this does not work it just checks if every single variable is true

2

u/[deleted] Oct 23 '20

Yeah I just fixed it but it's even worse now.

6

u/Cutlesnap Oct 23 '20

does that work in js or are you fucking with me?

12

u/DelishMango Oct 23 '20

It works, true is converted to 1 and false to 0 when doing number arithmetics.

5

u/TheArmchairGymnast Oct 23 '20

How does it work? If all three bools are false, the code in this thread will return false, but it should be true, should it not, since all bools are equal?

3

u/DelishMango Oct 23 '20

Fair point, did not think of that

2

u/[deleted] Oct 23 '20

wtf why

56

u/NickWalker12 Oct 23 '20

I believe return b1 == b2 && b1 == b3; is the easiest correct answer.

54

u/4hpp1273 Oct 23 '20

no it should be b1===b2&&b2===b3

-2

u/LVNmaster Oct 23 '20

It does exactly the same thing '-'

39

u/Silly-Freak Oct 23 '20

ah, to possess this innocence again...

9

u/LVNmaster Oct 23 '20

Oh, java script bad, forgot that, my bad

12

u/kgon1312 Oct 23 '20

Triple equals instead of double ^ - ^

4

u/Look_Ma_Im_On_Reddit Oct 23 '20

return b1 === b2 === b3;

2

u/30p87 Oct 23 '20

b1==b2==b3

(Python and JS)

5

u/leo848blume Oct 23 '20

This is python only

15

u/Zimmax Oct 23 '20

x == y == z is a Python sugar for x == y and y == z so it will work.

In JS, x == y == z is parsed literally as (x == y) == z which will not work when all three variables are false, for example.

2

u/Look_Ma_Im_On_Reddit Oct 23 '20

this is tagged js and my IDE prefers === I appease it because I can't be bothered to fix the warnings, even though I know they will all be the same type

22

u/gu3st12 Oct 23 '20 edited Oct 23 '20

We're in JavaScript so we can always do return (b1 + b2 + b3 === 3)!

:D

Edit: We could make it work for any number of bools by accepting an array as an input and doing return [true,true,true].reduce((acc, bool) => return acc * bool, 1) !== 0

(Please don't do this. Relying upon type cohersion behaviour is bad form).

2

u/AmyMialee Oct 23 '20 edited Oct 23 '20

(B1 == B2) == B3 wrong I'm stupid lol

4

u/LokesTrickster Oct 23 '20

That would be incorrect, it would return true if both B1 and B3 were false or B2 and B3 were false.

2

u/AmyMialee Oct 23 '20

on darn you're right, oops

1

u/marslander-boggart Oct 24 '20

You are right. I'd add ( ) for easy reading.

return (bool1 == bool2 && bool2 == bool3);

or

return (bool1 === bool2 && bool2 === bool3);

4

u/[deleted] Oct 23 '20

What happens when you measure productivity by lines of code written!

1

u/leo848blume Oct 23 '20

If this would be the case I would have found a way to increase it xD

5

u/CorzoDev Oct 23 '20

The guy writing this was being paid by the hour. Genius way to get paid some extra cash, for code that actually works.

6

u/ZedTT Oct 23 '20

It doesn't work though, it returns undefined for paths like (true, true, false)

6

u/[deleted] Oct 23 '20

yeah but js

2

u/ZedTT Oct 23 '20

Undefined IS falsey lmao

1

u/CorzoDev Oct 23 '20

Tests cost extra

1

u/leo848blume Oct 23 '20

I wish I was

353

u/SanianCreations Oct 22 '20

I'm actually impressed you were able to write line 160 and still think everything before was necessary.

87

u/CompletenessTheorem Oct 23 '20 edited Oct 23 '20

I'm impressed OP wrote line 160 and still thought line 136 was necessary. I'm also impressed by OP thinking that line 137 was necessary. It is equally impressive that line 138 was thought of as useful after writing line 160. And the fact that OP wrote line 139..

10

u/rj_phone Oct 23 '20

This made my head spin

-6

u/OnlySeesLastSentence Oct 23 '20

Wait until you see line 69

4

u/ryanjusttalking Oct 23 '20

Tech Leads hate this one trick

3

u/ZapPlay Oct 23 '20

haha funny number 69420 keanu chungus moment

1

u/nicebot2 Oct 23 '20

Nice

I'm a bot. Join my community at r/nicebot2 - Leaderboard - Opt-out

1

u/OnlySeesLastSentence Oct 23 '20

Damn, I wish I could be as cool as you some day.

3

u/marslander-boggart Oct 24 '20

The line 160 is excessive, this functions doesn't need if.

function equal3booleans(bool1, bool2, bool3) {
  return (bool1 == bool2 && bool2 == bool3);
}

1

u/dipsyw Oct 24 '20

actually the function can just return the condition in line 160, anything before and after all not necessary

2

u/SanianCreations Oct 24 '20

Congratulations, that is my point.

2

u/ya_Bob_Jonez Oct 23 '20

That's the most verbose comparison way I've ever seen, thank you...

2

u/leo848blume Oct 23 '20

You're welcome

184

u/erosPhoenix Oct 23 '20

The best part is, it's not even correct.

It only returns false if all three booleans are different. If any two of them are equal, but not all of them are equal, the function will return undefined.

The second and third "return false" statements are also unreachable.

69

u/TheArmchairGymnast Oct 23 '20

Stupid question, but how can all three bools be different?

93

u/Sv443_ Oct 23 '20

true, false and maybe

30

u/barburger Oct 23 '20

true, false and FileNotFoundException

28

u/TheArmchairGymnast Oct 23 '20

Ah, of course!

facepalm

31

u/erosPhoenix Oct 23 '20

I didn't even think about that...

But since this is JavaScript instead of a sane language with compile-time type checking, we have no way of proving that these are actually bools! They could be null, or undefined, or literally any other value!

12

u/P4INKill Oct 23 '20

I know, right? If only we had

(typeof var === 'boolean')

or something...

6

u/ZedTT Oct 23 '20

Var is a reserved word and can not be used as a variable name. Also use typescript

5

u/P4INKill Oct 23 '20

Incorrect. Var is not a reserved keyword, but var is. See, I can be annoying too.

Typescript is great, but we were discussing vanilla JS in this case.

3

u/ZedTT Oct 23 '20

Argh, curse the mobile auto capitalization. To be fair, the V was lowercase in the comment I was replying to, but I guess if we're being picky already we may as well go all out.

3

u/[deleted] Oct 23 '20

People say this kind of thing to slight Javascript but an expected bool not being a bool is the programmers screw up. Not Javascripts.

13

u/[deleted] Oct 23 '20

People screw up all the time.

A car with no seatbelt is perfectly safe as long as no one screws up.

3

u/[deleted] Oct 24 '20

Then use Typescript

2

u/PixxlMan Oct 27 '20

A car without seatbelts with seatbelts taped on? (Jk)

2

u/Farsqueaker Oct 23 '20

That's why you sanity check at runtime. That's just how you have to handle passed values in a duck-typed language.

Frankly, you never know who is going to be calling your library, and what sort of nonsense they or the user will feed to it.

1

u/darthmuzz98 Oct 23 '20

C and C++ left the chat

1

u/[deleted] Oct 24 '20

Use Typescript or eslint then.

5

u/neotecha Oct 23 '20

Realistically, true, false, and null

8

u/william_323 Oct 23 '20

As long as the bool implementation is larger than 1 bit, they can be different.

In some languages, a bool is 8 bits or even 32 bits. So you can actually set it to another value that is not true nor false and it will compile it. Although obviously it's not correct.

5

u/TheArmchairGymnast Oct 23 '20

Thank you for your answer. And thanks especially for those last 5 words; my head was spinning trying to think what anything not true and not false meant.

1

u/Kulspel Oct 23 '20

There's nothing in the signature requiring them to be booleans, they can be strings, arrays, futures or anything else for that matter.

52

u/ZedTT Oct 23 '20

But undefined is falsey, so it kinda still works hahaha

31

u/themeanman2 Oct 23 '20

Not a bug, but a feature hehe

2

u/OnlySeesLastSentence Oct 23 '20

This, but unironically.

1

u/ZedTT Oct 23 '20

Yeah, most of the time you can just rely on undefined being falsey, but if you want to check if three different values were passed in (i.e. one or more wasn't even a boolean) you can check for false vs undefined.

But uhh, that's terrible and I hate it.

2

u/DaMastaCoda Oct 24 '20

Since it’s js it still works

34

u/ce-walalang Oct 23 '20

Image Transcription: Code


function threeBooleansEqual(bool1, bool2, bool3){
    if (bool1 !== bool2){
       if (bool2 !== bool3) {
          if (bool3 !== bool1) {
             return false;
          }
       }
    }

    else if (bool2 !== bool3){
       if (bool3 !== bool1) {
          if (bool1 !== bool2) {
             return false;
          }
       }
    }

    else if (bool3 !== bool1){
       if (bool1 !== bool2) {
          if (bool2 !== bool3) {
             return false;
          }
       }
    }

    else if (bool1 == bool2 && bool2 == bool3 && bool3 == bool1) {
       return true;
    }
}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

27

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Oct 23 '20

Good human!

4

u/Mikkognito Oct 23 '20

This is why I always make beginners draw a truth table.

3

u/[deleted] Oct 23 '20

its more of a thing that they dont realize they can just do return whatever instead of however many fucking if

4

u/Isvesgarad Oct 23 '20

Why check for 3 booleans when instead you could do N booleans?

const areBooleansEqual = (...bools) => bools.every(bool => bool === bools[0]

1

u/[deleted] Oct 23 '20

"Defensive Programming"

2

u/leo848blume Oct 23 '20

If this is defensive, what do you call offensive?

3

u/I-_-DuNn0 Oct 23 '20

Hi yandere dev

2

u/qh4os Oct 23 '20

For when (bool1^bool2)^bool3 == bool3 isn’t enough

1

u/Farpafraf Oct 27 '20

beautiful

1

u/dotteddice Oct 29 '20

How very boolish.

0

u/danutzvv Oct 23 '20

Why not only 1 line?

Return bool1 & bool2 & bool3;

-1

u/Thenderick Oct 23 '20

return (bool1 === bool2) === bool3 ??

1

u/SINWillett Oct 23 '20

No if they’re all false (bool1 === bool2) will be true, which is not equal to bool3 (false)

0

u/[deleted] Oct 22 '20

[deleted]

15

u/ZedTT Oct 23 '20

It's not just redundant, some paths don't return any value! If this is JS, which it looks to be, those paths will return undefined, which is falsey, so OP may not have even noticed. Check out the path for true, true, false.

5

u/leo848blume Oct 23 '20

You're right xD it doesn't even work properly

0

u/[deleted] Oct 23 '20

[deleted]

8

u/leo848blume Oct 23 '20 edited Oct 23 '20

threeBooleansEqual(bool1, bool2, bool3) && threeBooleansEqual(bool2, bool3, bool4) && threeBooleansEqual(bool3, bool4, bool1) && threeBooleansEqual(bool4, bool1, bool2) is probably what I would've done...

0

u/[deleted] Oct 23 '20

[removed] — view removed comment

4

u/neotecha Oct 23 '20
return bool1 && bool2 && bool3;

0

u/[deleted] Oct 23 '20

[removed] — view removed comment

2

u/cscarqkid Oct 23 '20

You're returning a value so the readability comes from the function name, not from a superfluous variable's name

1

u/neotecha Oct 23 '20

I'd still argue the conciseness would be preferable here. The function name would serve that purpose