The Cuuuuube
link
fedilink
English
166d

been programming since 2008. the fuck is an elvis operator?

@dev_null@lemmy.ml
link
fedilink
3
edit-2
4d

It’s a shorthand for writing this:

variable = if (input != null) input else default

This is equivalent:

variable = input ?: default

The answers confusing it with the ternary operator are wrong.

Ternary if?then:else

The Cuuuuube
link
fedilink
English
46d

gotacha. i’ve only ever heard them called ternaries. maybe i’m old. maybe i’m too young. definitely one of the two

It specifically refers to this shorthand ?: that works like this:

$value = $thing_that_could_be_truthy ?: 'fallback value';

# same as

$value = $thing_that_could_be_truthy ? $thing_that_could_be_truthy : 'fallback value';

The condition is also the value if it is truthy

@jerkface@lemmy.ca
link
fedilink
English
86d

Been programming since the 80s, ditto.

Create a post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
  • 1 user online
  • 159 users / day
  • 317 users / week
  • 704 users / month
  • 2.84K users / 6 months
  • 1 subscriber
  • 1.57K Posts
  • 34.8K Comments
  • Modlog