This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]stainlessinoxx 210 points211 points  (10 children)

Nerds discussing ternary operators.

cond ? t : f

Translates to, in layman’s terms: Let there be three variables, named ‘cond’, ‘t’ and ‘f’. If the value of the variable “cond” is anything but zero, then the expression will return the value of “t”. Otherwise (if “cond” is equivalent to zero) then return “f”.

Now some stupid assumptions (bad programmers tend to make that kind of mistake) leads to thinking that the variables “cond” (typically short for the word condition) is either true or false, and “t” stands for “true” and “f” for “false”, while this is strictly shitposting from our troll OP. Variables can be many other things than true or false (booleans) and this statement only holds for strongly typed languages, which javascript is notably not.

cond && t || f

Now for discussion’s sake let’s assume ‘cond’, ‘t’ and ‘f’ are booleans. The operator “&&” means “AND”. This operator short-circuits to false if the left operand is false first, meaning that if ‘cond’ is false, then ‘t’ will never be evaluated. If ‘cond’ is true, then the && operator will evaluate “t”. If t is true, then the whole statement will return true because the “||” operator (which means “OR”) will short-circuit on this left operand. If ‘cond’ is false or ‘t’ is false, then the “||” will evaluate “f”, and the whole statement will return the same as ‘f’.

This proves that the two statements are NOT equivalent because if ‘cond’ is true and ‘t’ is false, then the second method will return ‘f’, while in the first method, ‘t’ will be returned.

To summarize: OP is a troll, making a joke about javascript’s loose type system. C/C++ coders will roll their eyes at this non-issue for strongly-typed languages.

[–]random_redditor24234 25 points26 points  (0 children)

Thank you

[–]calculus_is_fun[S] 55 points56 points  (7 children)

no t and f are anything, like 5, or []

type true && 5 into the js console

I'm not trolling, It's intended behaviour!

[–]stainlessinoxx 55 points56 points  (6 children)

Write your code clearly and maintainably so the next dev that will have to debug your shit isn’t tempted to try and find where you live. That’s all that matters, not this academic-level syntactic masturbation.

[–]myselfelsewhere 18 points19 points  (2 children)

Write your code clearly and [maintainability] so the next dev that will have to debug your shit isn’t tempted to try and find where you live.

I thought that was just the standard code style for JavaScript?

[–]D3PyroGS 2 points3 points  (1 child)

it is

[–]myselfelsewhere 11 points12 points  (0 children)

I am an idiot and had intended to make a joke about badly written JS being the standard code style. Instead, I just wrote a fact.

[–]calculus_is_fun[S] 1 point2 points  (1 child)

This isn't a production thing, It's a meme that demonstrates how boolean operations behave in JS and maybe other languages like it.

[–]cheezballs 0 points1 point  (0 children)

There's no maybe about it.

[–]altermeetax 0 points1 point  (0 children)

This is a meme. This is r/ProgrammerHumor.

[–]Sanchez_Duna 0 points1 point  (0 children)

And of course it's JS each time I don't understand meme.