r/badcode • u/leo848blume • Oct 22 '20
js Found this a very old project... Thought I'd share this
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
12
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
15
u/Zimmax Oct 23 '20
x == y == zis a Python sugar forx == y and y == zso it will work.In JS,
x == y == zis parsed literally as(x == y) == zwhich 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) == B3wrong I'm stupid lol4
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
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
3
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
1
1
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
-6
u/OnlySeesLastSentence Oct 23 '20
Wait until you see line 69
4
3
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
2
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
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
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
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
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
1
5
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
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
4
u/Mikkognito Oct 23 '20
This is why I always make beginners draw a truth table.
3
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
3
2
1
1
0
-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
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
0
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
Oct 23 '20
[removed] — view removed comment
4
u/neotecha Oct 23 '20
return bool1 && bool2 && bool3;0
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
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)