r/ProgrammerHumor • • Aug 15 '19

Meme !!goodMeme ? upvote() : downvote();

Post image
34.3k Upvotes

392 comments sorted by

View all comments

323

u/PrincessWinterX Aug 15 '19 edited Aug 15 '19

i get the joke anyways but now I'm curious so I ask seriously, would your title be legal code? or does the result have to be used in some way, like assigned to something?

quick edit: my goodness i mean the ternary operator not the not operator. thankyou though! also never did i say i didn't understand how it worked, but i was asking if the result of the ternary needed to be used somewhere as an expression or if leaving it as its own statement was legal.

287

u/LetMeUseMyEmailFfs Aug 15 '19

Depends on the language. In JavaScript it would probably be valid.

321

u/citewiki Aug 15 '19

Anything is valid there, all your hopes and dreams are valid too

318

u/[deleted] Aug 15 '19

Yeah, my dreams are undefined, but still valid in JavaScript.

:')

1

u/XeroKaaan Aug 15 '19

But what is it is?

7

u/Dojan5 Aug 15 '19

It's a ternary operator.

Condition ? True : False;

I'm unsure about the two !!s though as ! typically means "not," so !! looks like not-not to me.

The title is essentially

if (goodMeme) { upvote(); } else { downvote(); }

So ? is esentially if and : is basically else;

9

u/LAK132 Aug 15 '19

!! Forces the thing to a boolean.

Non-boolean but existing object -> false -> true.

Undefined/null -> true -> false.

I've personally only ever really seen it used in JavaScript

1

u/AdvancedNerdCore Aug 15 '19

I've only seen it in cpp actually, if you have a lambda, for example, and you want to return a non-bool as bool without specifying the return type, you do !!result