DEV Community

Discussion on: ✔||🤢 Commit or Vomit | nested ternary operator

Collapse
 
lexlohr profile image
Alex Lohr

It's JS, not C, and the secret to reading it is in the rythm:

Q1
? Y1
: N1/Q2
? Y2
: N2;
Enter fullscreen mode Exit fullscreen mode

Once you're used to the pattern, it is more legible than the other version.

That being said, I really like the verbose ternary of Python and Rust. Unfortunately they'll never be supported by ECMAscript.

Thread Thread
 
idanarye profile image
Idan Arye

Rust is a different story. It's not a ternary conditional operator there - it's a full blown if expression and any prettifier would format it with proper indentations:

let H = if C == 0 {
    None
} else if V == r {
    Some((g - b) / C)
} else if V == g {
    Some((b - r) / C + 2)
} else {
    Some((r - g) / C + 4)
};
Enter fullscreen mode Exit fullscreen mode