ternary opruleator

cross-posted from: https://lemmy.blahaj.zone/post/31679150

proportional reaction

78 points · 19 comments · view on lemmy.world

19 Comments

SubArcticTundra@lemmy.ml · 17 pts · 342d (3 replies)

Meanwhile Rust, where every control flow has a return value

eupraxia@lemmy.blahaj.zone · 15 pts · 341d (2 replies)

it's great, instead of ?: bullshit we just use if/else like it always should have been:

let x = if y > 3 { 7 } else { 4 };

SubArcticTundra@lemmy.ml · 3 pts · 341d

Yep

JackbyDev@programming.dev · 1 pts · 341d

Yeah if-then expressions are really nice.

cupcakezealot@piefed.blahaj.zone · 13 pts · 342d (1 reply)

its not my fault return apple == red ? true : false is so cute. :3

yetAnotherUser@discuss.tchncs.de · 3 pts · 340d

Not object oriented enough

return fruit.isColor(Color.RED)

Jankatarch@lemmy.world · 9 pts · 342d (1 reply)

That

return a === b ? true : false

Is bothering me way too much. Just

return a === b

SeductiveTortoise@piefed.social · 9 pts · 342d

return true === (a === b) ? true : false

Now that I think of it, the return of false should be a casted integer. Just to add horror.

SparroHawc@lemmy.zip · 9 pts · 342d

Schmoo@slrpnk.net · 8 pts · 342d

You can have my ternary operator when you pry it from my cold, dead hands.

Ephera@lemmy.ml · 7 pts · 342d

I kind of love the idea of the ternary operator. C is all about being a procedural language, except this is of course a lie, because it does have mathematical expressions.

But then they realized that it's not just mathematical expressions where being purely procedural is kind of shit, so they wanted to introduce if-else expressions.

But oh no, we can't have it look like the if-else control flow element. This is where madness functional programming lies. Let's make it look like a mathematical expression instead, by choosing the ugliest syntax known to man.

But what really makes it a meme is that other languages decided they needed to look like C, so they copied this terrible design and even though JavaScript has almost nothing to do with C anymore, webdevs in particular get to suffer from the unreadable syntax all the more.

dave@programming.dev · 5 pts · 341d

Ternary operators are the one thing I really miss from Go.

MistressRemilia@lemmy.sdf.org · 5 pts · 342d (4 replies)
# I mean, Crystal does have a ternary operator, too...
bar = case something
      in "aaa" then true
      in "bbb" then false
      end

foo = if bar
        69
      else
        42
      end
;; Or CASE or IF or COND or whatever
(let* ((bar (ecase (something)
              (:aaa t)
              (:bbb nil)))
       (foo (if bar 69 42)))
  )
ricecake@sh.itjust.works · 7 pts · 342d

Yeah, the problem isn't "conditional expressions", it's "terse syntax", and operator rules that you need to just memorize because they're special and different. Also being limited such that you need to nest the extremely deeply if you, for whatever, you need to have a complex inline condition.

I like the case expression , although I mostly know it from erlang.

Sv443@sh.itjust.works · 6 pts · 341d (2 replies)

I've been called slurs before for using immediately invoked anonymous function expressions with switch cases in JS

const [val1, val2] = (() => {
  switch(whatever) {
    case "foo": return [1, 2];
    case "bar": return [3, 4];
    default: return [0, 0];
  }
}))();
gandalf_der_12te@lemmy.blahaj.zone · 3 pts · 341d (1 reply)
const [val1, val2] = ({
  "foo": [1, 2],
  "bar": [3, 4],
})[whatever] || [0, 0];
Sv443@sh.itjust.works · 2 pts · 340d

🤫

Sv443@sh.itjust.works · 4 pts · 342d

TypeScript library devs are shivering in cold sweat right now

gandalf_der_12te@lemmy.blahaj.zone · 4 pts · 341d

ternary operator rules though