4 ms·
I (a different person replying to your post) don't mind ternary operators, but I do prefer if-expressions which convey the same thing semantically but can be ni
by zogrodea 3y ago
I (a different person replying to your post) don't mind ternary operators, but I do prefer if-expressions which convey the same thing semantically but can be nicer to read (and also way better when chaining else-if ladders - nested ternaries can be a nightmare to read). https://stackoverflow.com/a/46843369 https://stackoverflow.com/a/46843369
- IlliOnato 3y agoChained ternaries are quite readable when formatted as tables... a = (x < 5 ) ? 'LESS' : (x > 5 ) ? 'MORE' : 'SAME';
- 8372049 3y agoTo me personally, the use case for ?: is selecting a value for a const: const value = cond ? a : b; Using an if instead would mean you can't make it const, and is usually more convoluted to read. For pretty much all other use cases, I agree that ifs are better.
- kevindamm 3y agoIn that case you can have the result of a function call be assigned to the const, and the function be the if/else for the value. With a well-named function it can even be more readable than the ternary.
- IlliOnato 3y agoHaving to create a function where a simple expression would suffice is "typing in mittens" programming...
- kevindamm 3y agoFair enough.. I actually use ternary sometimes, especially in brief return statements, and especially when both results are simple values. I only really avoid it when the result expressions are multiline. I just felt like pointing out the obvious when I saw GP's comment.
- zogrodea 3y agoI was confused by your reply when I first read it (didn't quite understand it), but I think it comes from a miscommunication when I had intended if-expressions (if statements that return a value) while you had if-statements in mind. For example, the following F# code (you can run `dotnet fsi` in a terminal to try it out, if you have dotnet installed) is equivalent to a ternary expression: // the value of x is "is_not_lower" after evaluation (x is a constant/immutable value, just like a 'const' value in JS) let x = if 0 > 1 then "is_lower" else "is_not_lower" Which is equivalent to the following (in JS): const x = 0 > 1 ? "is_lower" : "is_not_lower" In F# (and also Kotlin which I linked before), you can have clean code with if-elif-else ladders while the equivalent (using nested ternaries) would be hard to read. For example: // the constant/immutable variable "comparison" is either "HIGHER", "LOWER" or "EQUAL" depending on the branch taken let comparison = if a > b then "HIGHER" elif a < b then "LOWER" else "EQUAL" In JS land (and also most common programming languages which have plain if-statements), I agree a ternary would often be preferrable as long as it's not nested.
- IlliOnato 3y agoI would say that languages where "if" can be used as expression provide a different (perhaps better) syntax for the same ternary operator. a = if x then y else z is just a more verbose version of a = x ? y : z; Raku uses yet another form of the same operator: a = x ?? y :: z; Syntactic sugar :-)
- lizmat 3y agoIt's actually a = x ?? y !! z And that's because a single : is used for many other semantics in the Raku Programming Language. And ?? and !! standout better in code. https://docs.raku.org/language/operators#infix_??_ https://docs.raku.org/language/operators#infix_??_!!
- IlliOnato 3y agoYou are right, of course. Sorry! "Colon belongs to Larry" :-) I think I made this mistake because :: was used in one of the drafts for Perl 6, but the final specification uses !!