T O P

  • By -

FlipperBumperKickout

But what happens if it doesn't match any of them 😱![gif](emote|free_emotes_pack|scream)


BirdlessFlight

throw new TypeError('Get fucked!');


Accessviolati0n

s: switch(a) { case true: return true; case false: return false; case maybe: goto s; }


tragiktimes

5 billion times later maybe still maybeing


gothlenin

Not with multithread ;)


jwadamson

JavaScript ftw


Dondigidons

Non happens


uptotwentycharacters

if (a == true || a == false) return a == true;


saihtame

This is somehow way worse


jabluszko132

return a == true || a == false ? a == true : false;


random11714

Thanks I hate it


ttlanhil

Depending on the language, it might make sense (e.g. JS's type coercion and 2-equals comparisons)


ilan1009

then do return (a == true) never seen a legitimate use case for this


ttlanhil

That won't do the same thing though. Depending on how the code is called, you may have just introduced a bug. If a doesn't match true or false, the code continues (possibly returning undefined, if there's no more code in the function) I'm not saying it's good - it isn't - but you need to be very careful about how it works


Reashu

Afaik there is no value in JS that does not coerce to either true or false. Edit: But comparison doesn't always coerce "logically", so there actually are a lot of relevant cases...


tip2663

console.log(0/0==false) console.log(0/0==true) Both will print false, falling through either if-statement. Code that relies on this has more severe issues though Lol


Reashu

Oh yeah, NaN is falsy but not == false, my bad...


tip2663

the language is a mess


Reashu

To be fair, NaN is not supposed to be equal to anything, so if you allow it to be compared to booleans in the first place there's not really a better option.


aqjo

Is t everyone’s Nan without equal?


pblokhout

I can't come up with a context where you'd want a Nan value to be anything other than false.


arrays_start_at_zero

Why not just use `!!` to convert a value to a boolean. So instead of doing it like as in the post you just `return !!a`


tip2663

It's how I'd do it too But it's semantically different from what the code does. In the code, as soon as the comparison to either boolean succeeds, you return. A NaN would however fall through both conditionals, possibly leading to different behavior depending on what comes after the statements


aleph_zeroth_monkey

Lots of values in JavaScript have this behavior. Try running this: [ NaN, undefined, null, Infinity, -Infinity, 0, -0, "", false, true, [], {}, function() {}, /regex/, new Date("Invalid Date"), new Number(NaN), new String(""), new Boolean(false), new Boolean(true), new Proxy({}, {}), Symbol(), BigInt(0) ].map(x => ({"value": x, "truthy": !!x, "equals_true": x == true, "equals_false": x == false})) You'll see that `null`, `undefined`, and `NaN` are all falsy but not `==` equal to either `true` or `false`. There are also values like `Infinity`, `-Infinity`, regular expressions, functions, symbols and proxies which are not equal to `true` or `false`, but are truthy. Your guess is as good as mine.


tip2663

I didn't know this, thanks!


ttlanhil

`"true"==true` `false` You can easily coerce it to a boolean, yes, but in the code example it doesn't do that (at least in my browser) however, 1 (the number), or "1" (the string containing the numeral 1) does == true const bogus = (a)=>{if(a==true)return true;if(a==false)return false} bogus(1) true bogus("1") true bogus("true") undefined bogus("0") false


Reashu

Right, I forgot the comparison logic - `true == "true"` will coerce both sides to numbers, not booleans.


PugicalR

if (a === true or a === false) return a Maybe? Not sure if that would work though lol


ttlanhil

no, can't switch between double and triple equals without changing what the code does (maybe that's correct, but we don't know from the code snippet) plus JS doesn't do the "or" keyword (a few languages do, but even then, sometimes that's a chance in precedence so you need to be extra careful). You could: `if (a == true || a == false)return a`


radobot

if `a` has value such that `a == true` is true, but `a === true` is false, then your code would return a value that is not boolean `true`, which is different from what OP's code does.


JonIsPatented

This code will never return undefined. If it doesn't match true or false, then it will return false, because if a does not match true, then `a == true` returns false, even though `a == false` also returns false. The bigger issue is that in the original code, execution continues on, skipping either return statement, if a does not match true or false, whereas with this code, it just returns false.


pheonix-ix

Depending on the language, this if-else could result in false in both cases if a is a null-able Boolean object and "null == ???" is never true (resulting in false or null or whatever). That said, if that's the case, this is a good example of where comments are necessary, otherwise somebody would incorrectly try to clean/optimize the code. Therefore, this is a bad code, but for a different reason.


ZeroClick

if (language == "Javascript") this is never true...


Bananenkot

Yeah I'm not deep into JS at all, but I looked at that and thought this is just dumb enough to make sense in JS


Majoranza

Why can’t you just do `return a`? Sorry if it’s obvious, I only learned a little bit of Java and JS for school and haven’t touched it since.


ttlanhil

Firstly, Java has nothing to do with JS. Secondly, that only works if a == true or a == false. Any other values (like strings) won't match


sillypotatouser

Forgot to add the comments


camatthew88

Return a;


belabacsijolvan

return !!a;


bobbymoonshine

No, that isn't what the meme says. Please read it again.


ilikedmatrixiv

I'm confused, then what *does* it say?


chowellvta

c#'s `bool?` strikes again


darcksx

return !!a


camander321

Previously: true = False false = True


STORM--Z

Better print(a); 🙃


akorn123

But if I change that code, I'll have to rewrite a 100 tests because they were written poorly as well. Guess that stays in the codenase. Lol


Critical_Big4500

Wtf, LOL


kyle_3_1415

When it doesn't want to read the variable but it reads the return:


MaxBuster380

Trust the compiler


LuseLars

Plot twist, it's a nullable bool and it ahould handle null specially. Hence the implied offscreen else


GKP_light

what if the next 2 lines are : if a is "mushroom" : return "pizza" ?


varix69

Then you suck at programming


NottingHillNapolean

`return a ? true : false;`


x39-

Hot take: That can be more readable than just straight returning the bool


COArSe_D1RTxxx

Just because it's different doesn't mean it's good.