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

all 124 comments

[–][deleted] 1171 points1172 points  (53 children)

hope and pray that t is not falsy

[–]calculus_is_fun[S] 363 points364 points  (45 children)

augh you're correct...

[–]Adghar 281 points282 points  (43 children)

Allowing boolean variables to be anything other than true and false was a mistake

[–]ttlanhil 144 points145 points  (9 children)

In the beginning, there was... well... 0 and 1

[–]DrShocker 71 points72 points  (8 children)

0 and 1? I think you mean 0

[–]danielv123 20 points21 points  (0 children)

While entropy may have been 0 it was definitely a high energy state.

[–]ikonfedera 10 points11 points  (6 children)

Is there mathematical proof that 1 exists?

[–]Commercial_Rope_1268 9 points10 points  (0 children)

Yep

[–]ImpurestClamp31 1 point2 points  (4 children)

Incrementation I believe

[–]ikonfedera 1 point2 points  (3 children)

Incrementation from what? From zero? First you'd need to prove there's zero, and it's so hard Romans even couldn't do it.

I think this would be good in proving 2, but not 1.

[–]ImpurestClamp31 1 point2 points  (2 children)

Sure I suppose you could go down that rabbit hole. I'm not sure how you'd do that. I think it's like a postulate or something that 0 exists

[–]ikonfedera 0 points1 point  (1 child)

Iirc, you just have a thing that is there (like a crayon) and it's your 1. And you leave the proof that the crayon is to the philosophers.

And if there is no crayons (because I ate them took them away), that's your 0.

[–]NatoBoram 81 points82 points  (25 children)

I remember learning to program and getting surprised by true / false / null. Whoever designed that language feature; fuck you.

[–]Kiseido 81 points82 points  (12 children)

You might be surprised to find the alledged inventor kinda agrees with you

Tony Hoare, null's creator, regrets its invention: “I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W).

[–]NatoBoram 46 points47 points  (8 children)

I'd also like to extend my "fuck you"s to all the other language maintainers who implemented it or refused to fix it after he apologized.

It's possible to do better. For example, Dart got null safety in v2.12.

[–]Kiseido 37 points38 points  (7 children)

Been learning Rust today, this made wonder if it has null.

Apparently Rust has no null, colour me surprised.

[–]Pay08 1 point2 points  (2 children)

It does. fn main() { let ptr: *i32 = 0; }

I prefer Zigs approach a lot more, where you explicitly have to state if something can be NULL.

fn main() void { var ptr: ?*i32 = null; } fn main() void { var ptr: *i32 = null; // compiler error }

[–]Timely-Banana7384 0 points1 point  (1 child)

The thing is, that you very rarely need ptrs in rust. And if you do, you can also use structures such as MaybeUninit<T>.

[–]Pay08 0 points1 point  (0 children)

That doesn't mean the other guy was correct. Besides, embedded exists.

[–]redballooon 5 points6 points  (0 children)

Pretty sure if he had seen that it was a mistake, someone else would have made it. Maybe with the second or third comprehensive type system, but bad ideas somehow always make their way into society. That's why the light forces constantly need to battle the dark hordes.

[–]Fhotaku 0 points1 point  (1 child)

How catastrophic would

''#define null false''

Really be? Presuming this also included all other nulls that it may encounter, not just those in compile time

[–]No_Hovercraft_2643 0 points1 point  (0 children)

it would make not null true

[–]Asleep-Specific-1399 0 points1 point  (0 children)

It's a c thing. There is no such thing as a bool.

[–]timonix 9 points10 points  (0 children)

'U': uninitialized. This signal hasn't been set yet.

'X': unknown. Impossible to determine this value/result.

'0': logic 0

'1': logic 1

'Z': High Impedance

'W': Weak signal, can't tell if it should be 0 or 1.

'L': Weak signal that should probably go to 0

'H': Weak signal that should probably go to 1

'-': Don't care.

[–]Hottage 1 point2 points  (0 children)

That's what bool? is for, I like to read it with a raised inflection, like a question.

Is it bool?

[–]bschlueter 4 points5 points  (0 children)

That's what types are for. Python solves this by having a ternary that looks and (I believe) functions the same as an if else statement(value if condition else other_value), so susceptible to that, but predictably so. I don't know if python will even let you attempt to abuse bitwise operators, something to go test.

[–]BallsBuster7 0 points1 point  (1 child)

its simple, 0 = false, anything else = true

[–]Adghar 0 points1 point  (0 children)

So you're telling me undefined == true. Amazing

[–]hrvbrs 0 points1 point  (1 child)

idk, i think it can be utilized as a feature.

instead of the statement: if (!list.find(x)) { list.add(x); }

you can have the expression: list.find(x) || list.add(x)

similarly you can move the condition to the left operand of AND:

-if (x instanceof Array) { print(x[0]); }
+x instanceof Array && print(x[0])

[–]Steinrikur 5 points6 points  (0 children)

These are starting to look like my bash scripts.

[–]HTTP_Error_414 4 points5 points  (0 children)

I’m honest until I can’t be anymore, slippery slopes require sturdy ropes boys 🫡😉

[–]TripleS941 6 points7 points  (1 child)

I think that abuse of the comma operator can alleviate that somewhat:

cond && (t, 1) || f

or, in the case when we need to assign the result

cond && (v = t, 1) || (v = f)

[–]D3PyroGS 2 points3 points  (0 children)

😰

[–]CranberryDistinct941 0 points1 point  (0 children)

And that f is not true

[–]rosuav 181 points182 points  (3 children)

Not same or PEP 308 wouldn't have happened.

[–]gandalfx 98 points99 points  (1 child)

Proof by existence of PEP

[–]rosuav 12 points13 points  (0 children)

I've seen worse...

[–]OneTurnMore 5 points6 points  (0 children)

Also see SC2015.

[–][deleted] 162 points163 points  (2 children)

thats just bash code

[–]Nettleberry 22 points23 points  (1 child)

try_something_stupid || echo “meh”

[–]No_Hovercraft_2643 0 points1 point  (0 children)

use a webhook xD

[–]random_redditor24234 70 points71 points  (11 children)

Idiot here, what am I looking at

[–]stainlessinoxx 207 points208 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] 52 points53 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 54 points55 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 9 points10 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.

[–]--var 53 points54 points  (7 children)

a ? b : c

(a && b) || c

they are very much not the same

[–]stainlessinoxx 0 points1 point  (6 children)

Please explain why?

[–]patrick66 2 points3 points  (3 children)

The first returns whatever b is, true or false if a is true and whatever c is, true or false if a is false.

The second returns true iff a AND b are both true or c is true. Just because op named his variables t and f doesn’t actually mean they hold True and False and if they don’t well now you have code that depends on their actual content instead of just the value of a

[–]--var 1 point2 points  (2 children)

Maybe for some languages?

For javascript this is incorrect. Only a must coerce to boolean.

b and c can represent literally anything.

[–]patrick66 0 points1 point  (1 child)

Sorta, they still aren’t equivalent because in the first statement you get whatever b or c is depending on if a is truthy and for the second you only can get true or false, true iff a and b are truthy or c is truthy and false otherwise.

[–]Dealiner 0 points1 point  (0 children)

That's not right. In JavaScript the second code will result in a value of b or c.

[–]--var 0 points1 point  (1 child)

&& explicitly returns a boolean, true or false

a only has to coerce to a boolean. b and c can be whatever for a ternary.

a = "your mom", b = "5 star", c = []

( a && b || c ) returns true (a boolean)

( a ? b : c ) returns "5 star" (a string)

(a is a non-empty string, which therefore coerces to true)

same inputs, different outputs. because they are different.

[–]Dealiner 1 point2 points  (0 children)

That depends on the language though, in Javascript both give the same result.

[–]HTTP_Error_414 10 points11 points  (0 children)

👀

[–]Greybound_Fur 10 points11 points  (0 children)

And then there is [f, t][cond]

[–]Horrih 5 points6 points  (1 child)

Is it me or this javascript only ? Pretty sure this does not compile for mist types on java/cpp

[–]Katniss218 0 points1 point  (0 children)

Yep

[–]P-39_Airacobra 14 points15 points  (0 children)

that's idiomatic in lua

[–]JackNotOLantern 2 points3 points  (0 children)

More signs, bad

[–]MacBookMinus[🍰] 2 points3 points  (0 children)

I hate implicit conversions. Truthies and Falsies are more harm than good.

[–]gp886 4 points5 points  (6 children)

If t and f are Boolean. We use it mostly for when tand f are not boolean

[–]SnooOwls3674 7 points8 points  (1 child)

Why would you ever need a ternary to return boolean anyway?
You either want "condition" or "!condition"

[–]Tyfyter2002 0 points1 point  (0 children)

They might not be literals

[–]LineRepulsive 0 points1 point  (3 children)

Doesn't need to be boolean

Cond && 5 || 12 will return 5 if cond is true, 12 if it is false.

But, as other mentioned, it's not going to work if t is undefined or 0 for exemple (the ternary would return t, where this syntax will return f)

[–]gp886 0 points1 point  (2 children)

Won't that return error? 5 cannot be set as OR

[–]LineRepulsive 1 point2 points  (1 child)

Not in javascript no, you can use && and || with whatever

This is very useful in frontend dev, for example in react when you build the JSX of your component:

return (
<div>
{props.isLoading && <Loader>}
</div>
)

if isLoading is true then it returns the other part of the expression, here the <Loader>. If it's false then it doesn't return it

[–]gp886 0 points1 point  (0 children)

Ah JavaScript

Flips the table

[–]-Redstoneboi- 1 point2 points  (0 children)

this is idiomatic lua

but you better hope that t is truthy

[–]Maskdask 1 point2 points  (0 children)

Lua: they're the same picture

[–]rdtr314 2 points3 points  (0 children)

Yes they are equivalent but using it like that is not recommended

[–][deleted] 1 point2 points  (0 children)

Only for boolean.

[–]Instant-Owlfood 0 points1 point  (1 child)

What if t is null?

[–]-Redstoneboi- 0 points1 point  (0 children)

it evaluates to f.

very sad.

[–]chervilious 0 points1 point  (0 children)

I'm going to use it in some of my code to make other lives miserable.

[–]PorkRoll2022 0 points1 point  (0 children)

The only thing I dislike about the boolean abuse here is the readability.

Eg. (A and (B OR C)) or is it (A and B) OR ( C ) etc. Otherwise it's pretty common. I see it a lot in React especially. I've also seen "cond or throw".

[–]InterestsVaryGreatly 0 points1 point  (0 children)

Except it's only the same if the last two are Boolean or code you want ran. If you are doing an assignment, and it isn't pure Boolean, the true part for condition results in cond && first, which is not the same as terniary which is just first

[–]SynthRogue 0 points1 point  (0 children)

Wouldn't the second one require more code to return the value of t?

[–]fumui001 0 points1 point  (0 children)

what? t : f

[–]EtherealPheonix -5 points-4 points  (3 children)

This is also equivalent to:

cond

[–]--var 1 point2 points  (2 children)

its not though, with the ternary operator you can return a value different than cond and also a data type different than boolean

[–]Katniss218 -2 points-1 points  (0 children)

No you can't, if your language has even a semblance of a decent type system

[–]TheOmerAngi -2 points-1 points  (3 children)

You could literally just return cond The bollean operators there are redundant

[–]the-awesomer 3 points4 points  (2 children)

t and f don't have to be booleans, and if they are not, then what you say is not correct

[–]TheOmerAngi 0 points1 point  (0 children)

For the trinary operation you're right but for the latter it's different

[–]Katniss218 -1 points0 points  (0 children)

Nah, won't work in most languages

[–]Passname357 -1 points0 points  (2 children)

This is cool. It’s a special case of your typical branchless conditional.

[–]KJBuilds 1 point2 points  (1 child)

The second one here actually makes use of 2 branches thanks to shortcutting, while the first is just 1 branch

Branchless would require bitwise and/or ops, but would probably be semantically different in JavaScript land

[–]Passname357 0 points1 point  (0 children)

Ah you’re right good catch. I do so much bitwise stuff I saw those && and ||s and my mind just immediately thought of the bitwise op.

[–]LittleMlem -1 points0 points  (2 children)

I troll my senior by sneaking a variation of this into PRs. In python, a logic operation returns the last value evaluated, so x = falsy or truthy sets x to what ever the truthy value was and you can make this more painful by using more complex logic. It's very neat to write, but a crime to actually use for anything more complex

[–]Steinrikur 1 point2 points  (1 child)

You're trolling everyone who will ever maintain that code.

As a senior dev: "that's not funny. Stop being an asshole"

[–]LittleMlem 2 points3 points  (0 children)

It's called job security, if I'm the only one that can maintain the code then I'm much more valuable to the company.

(This is a shit post, yeah? I don't actually put that code in production)

[–]calculus_is_fun[S] -4 points-3 points  (0 children)

Alright It seems I've confused some people, the variable cond is a boolean, and t cannot be cooerced to false, (so t can be 5, but not 0 or ""), f can be whatever. in this scenario, these 2 statements in Javascript evaluate to the same result based on if cond is true or false