Follow


Do you like using the ternary operator?

@nebunez
If it's a simple case and easy to understand what is going on, then yes. If it requires explanation or comment, I'd rather do it in a conditional statement. I feel like same about list comprehensions and "clever one-liners" in Python. We don't get charged by the line, and no one cares if you're clever 6 months from now.

@nebunez I do like, but it's highly dependent on the language.

Python ternary operator true_value if condition else false_value is way better than C condition? true_value: false_value (maybe 'cause I don't see the : as a separator).

@nebunez no need to even have a ternary operator if your IF is an expression, not a statement, and therefore can return a value

#JustLispThing

@phoe @nebunez

> no need to even have a ternary operator if your IF is an expression, not a statement, and therefore can return a value

That's the decision Rust went with (the language previously had a ternary operator and actually _removed_ it because if creates an expression).

Personally, I still miss the ternary. This is a valid expression, but doesn't look good (imo)

let foo = if condition {
"bar"
} else {
"baz"
};

(Of course, it looks better in lisp syntax)

@codesections @nebunez That's a damn nice decision. C should have done the same. C++ should have done the same. Java should have done the same. C# should have done the same.

@codesections @phoe @nebunez yeah I voted yes, but really I was thinking of if expressions from rather than a literal operator. You can also use case as an expression (equivalent to 'let match' in ), making it simple to write yes/no/default style code.

This poll also made me remember the first language I used; a scripting language for a game where the only control flow was a single-line ternary operator and goto statements.

Fun times trying to structure code in that language!

@codesections @phoe @nebunez for short stuff like in the example, it looks better if you put it in one line though

@codesections @phoe @nebunez VB 9.0 added `If (condition, truePart, falsePart)` (rather than a C-style ?: syntax)

Where is your God now

@chucker @codesections @phoe @nebunez But if you're going there, Scheme/LISP has
(if EXPR THEN ELSE)
and so you prefer to test positives and give both returns, even if just #t or #f.

I try never to test negatives even in C-like, I always do
if (EXPR) {
stuff(); // OK
} else {
badStuff(); // not OK
}

@phoe
Damn, should have had an "I use Lisp" option. I really want to get really into using Lisp for real projects, but I'm still at the "I understand how most Emacs configuration works" stage.

@nebunez Honestly though, it isn't just Lisp. I'm well aware that Erlang has it, someone else has also mentioned Rust. So it's many languages from different areas.

@nebunez @anathem I’m missing the “maybe” option.^^

I generally like them, but 6 level nested ternarys as a return value just for the sake of “I want ternary recursion” is... bad.

Sign in to participate in the conversation
Fosstodon

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.