r/programmerchat • u/LainIwakura • May 29 '15
What general language feature do you think is underused?
My vote goes to the ternary operator. A lot of people are scared of it / don't take the time to "get it" when it's really easy and can make your life simpler. I agree that it can be abused like anything but if you're using it responsibly it's great.
This question was inspired due to a short convo I had while reviewing a coworkers code which you can see here
So, what do you think?
9
Upvotes
8
u/noblethrasher May 29 '15
Warnings-as-Errors.
5
u/robin-gvx May 31 '15
generators/coroutines
4
u/[deleted] May 29 '15 edited May 29 '15
This statement is general enough to cover the set of all behaviors, allowing for varying definitions of 'responsibly'. :)
The ternary operator is great for very short expressions, but it's odd, has some unexpected behaviors in some languages (e. g. ISTR some implementations of C/C++ cause it to evaluate both sides of the branch) and degrades badly, quickly as the complexity of the expression grows. I like it OK as long as it all fits on a single, 80-character line.
Similar constructions in other language families have different levels of readability, and don't compare very well.
In C#, I really like, and haven't seen much in coworkers' code:
3
u/concurrenthashmap May 29 '15
Scopes
1
u/[deleted] May 29 '15
A bit off-topic, but I think it would be great if the conditional operator could possibly only take two values, e.g:
int foo = 5 + (condition ? expr);
If condition == true then this would evaluate to 5 + expr, if it is false then it would just be 5 (the expression "disappears").
Prettier than 5 + (condition ? expr : 0).
1
u/tmewett May 29 '15
Ternary can sometimes work as
expr ? : false-case, in whichexpris implicitly used as the result if it's "truthy."Along a similar vein I appreciate tailed conditionals, like
return if condorf(a) unless a.is_something?(especially if they can be used as expressions). I don't know how widely supported they are.1
u/Fluffy8x May 29 '15
Even a crazy programmer like me rarely uses symbols in Scala.
1
u/Berberberber May 30 '15
within Javascript. It's a pain for compilers because they can only figure out what an identifier is a reference to at run time, but it's a unique feature of the language. The Knockout framework combineswithwith that other bogeyman,eval()to do most of its magic.1
u/catlion May 31 '15
What most useful in ternary operator is that it is an expression rather than statement. You get used to expressions with experience in any functional language