4 ms·
In they article, they mention ternary notation. I personally wish that people didn't use it. While it's great for saving (a little) time, it really muddles th
by vital101 17y ago
In they article, they mention ternary notation. I personally wish that people didn't use it. While it's great for saving (a little) time, it really muddles the readability of the program IMHO. Its far too easy to simply skip over it as a simple assignment operation.
There are a lot of gems in this article though (aside from that).
- dpritchett 17y agoI agree. I've never used ternary notation regularly so it always takes me out of the flow when I'm trying to decipher someone else's source.
- mdg 17y agoI cringe when I see a simple if-else, which does nothing but assign a value to a variable depending on the condition. That is the best use case for a ternary operator, in my opinion. However, I have noticed that I tend to use them liberally, and have since reverted back to if-else when there is something more than "simple" going on in the condition or post-condition.
- MartinCron 17y agoOne of the more delightful moments in my programming career was seeing that ReSharper could convert ternary notation to the (sometimes) more readable if/then construction automatically. I'm pretty sure that other automatic refactoring tools can do the same.
- clutchski 17y ago"Its far too easy to simply skip over it as a simple assignment operation." This can be mitigated with parenthesis around the conditional statement. "ternary notation ... [is] great for saving (a little) time" I like it because it allows for immutable variables (enforced by fiat, of course), without writing four lines of "if ... else .." That being said, it can be abused: http://www.google.com/codesearch?hl=en&lr=&q=%22%3D%3D+true%29+%3F+true+:+false%22&sbtn=Search http://www.google.com/codesearch?hl=en&lr=&q=%22%3D%...
- Xurinos 17y agoLet me present an argument in favor of the ternary operator. tl;dr -- It is an abstraction on a conditional assignment pattern. We start with your "readable" approach: var a; if (condition(x)) { a = 4; } else { a = 5; } That has all the glory of line noise, repetition, and a declaration to make our language happy. This example is a little exaggerated. We can make our declaration more useful and our condition more of an exception: var a = 5; if (condition(x)) { a = 4; } That is still two assignments or, rather, the "a =" pattern repeated. And that is just for a trivial single branch. How can we abstract that out? You have two options. For simple conditions, we can do this: var a = condition(x) ? 4 : 5 ; It might be better to abstract it out like this: var a = value_for_a_with_environment(x); Sure, you may be using a ternary operator or if...else underneath the hood in value_for_a_with_environment(), but give that function a relevant name, and someone can get the gist without having to know the details. It could just be arrangement and syntax we despise. Here is another language's approach to the ternary operator: (setf a (if (condition x) 4 5)) Or with consideration given to the environment: (setf a (value-for-a-with-environment x)) Once I learned some of the value and abstractions of functional programming, I stopped feeling the ternary operator's use was ugly and realized it was more that I hated the line noise involved in using it. That colon looks a lot like a semicolon. Also, it is limited. In nontrivial code, I have seen cascades of ternary operators. var a = cond1(x) ? 4 : cond2(x) ? 5 : cond3(x) ? 6 : 7 ; or var a = cond1(x) ? 4 : cond2(x) ? 5 : cond3(x) ? 6 : 7 ; Pick your favorite arrangement. We really want a non-binary-looking structure like this: (setf a (cond (cond1 4) (cond2 8) (cond3 12) (t nil)))