Sasa is a highly driven full stack software developer with background in finance and accounting. A relentless problem solver who is passionate about finding elegant solutions to problems at hand.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Yes your example was a simple one, though simplicity is not a factor as any program can be expressed as a single expression, regardless of the simplicity. Therefore any example can be converted into a conditional ternary. Though it may is possible not look the cleanest or be the best solution.
But if we have code between the condition and the return statement, you cannot use a ternary oh enlightened one ;)
☝️ This statement can be proven false with this block of code:
constfoo=bar=>bar>0?(console.log('Execute some code here for YES'),'YES'):bar===0?(console.log('Execute some code here for MAYBE'),'MAYBE'):(console.log('Execute some code here for NO'),'NO')
To demonstrate my point that you can write any application as a single expression, you can read my article where I have written a fully functional Promise library, a fairly complex program, as a single function expression. dev.to/joelnet/challenge-program-w...
The good thing about using a ternary operator is that when the operator becomes complicated, it is a signal that your function needs to be broken down into smaller pieces. Where as with the if statement, you can easily add too many statements without receiving this signal and end up with a function that breaks the Single Responsibility rule.
Sasa is a highly driven full stack software developer with background in finance and accounting. A relentless problem solver who is passionate about finding elegant solutions to problems at hand.
Sasa is a highly driven full stack software developer with background in finance and accounting. A relentless problem solver who is passionate about finding elegant solutions to problems at hand.
I’m fine with the quick return for validation or a guard as long as it’s right at the beginning of a function.
I’d rather have one single exit point for my function though since having multiple outs adds to the chances that someone will add code between the two exits without realizing it.
A “better” solution:
# python 2 only. Cmp was taken from the code golfers in python 3.defsilly(bar):output={"-1":"NO","0":"MAYBE","1":"YES"}returnoutput[str(cmp(bar,0))]# LATE EDIT FOR MAXIMUM UNREADABILITY:defsilly(bar):return{"-1":"NO","0":"MAYBE","1":"YES"}[str(cmp(bar,0))]
If your function is equivalent to a nested ternary, then that’s a good use of early return. Anything more complicated needs to have the contents of the If statements extracted into methods to more clearly show off your now ternary equivalent ifelse
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Looks like the simplicity of my example stands in the way of the message I'm trying to convey.
If we take the examples literally there is no issue, a single line ternary
But if we have code between the condition and the return statement, you cannot use a ternary oh enlightened one ;)
Yes your example was a simple one, though simplicity is not a factor as any program can be expressed as a single expression, regardless of the simplicity. Therefore any example can be converted into a conditional ternary. Though it may is possible not look the cleanest or be the best solution.
☝️ This statement can be proven false with this block of code:
To demonstrate my point that you can write any application as a single expression, you can read my article where I have written a fully functional Promise library, a fairly complex program, as a single function expression. dev.to/joelnet/challenge-program-w...
The good thing about using a ternary operator is that when the operator becomes complicated, it is a signal that your function needs to be broken down into smaller pieces. Where as with the
if
statement, you can easily add too many statements without receiving this signal and end up with a function that breaks the Single Responsibility rule.Just because you can do something does not mean you should, have fun maintaining your library :)
Just because you shouldn't do something it doesn't mean you can't ;)
True as well :D
I’m fine with the quick return for validation or a guard as long as it’s right at the beginning of a function.
I’d rather have one single exit point for my function though since having multiple outs adds to the chances that someone will add code between the two exits without realizing it.
A “better” solution:
If your function is equivalent to a nested ternary, then that’s a good use of early return. Anything more complicated needs to have the contents of the If statements extracted into methods to more clearly show off your now ternary equivalent ifelse
I think OP's original example uses the conditions
== 0
,> 0
and< 0
, so a hash table wouldn't work for2
or-2
.I think if you look closer, you'll see that my solution is equivalent in output to the OP's example.
Here's a nastier one (also with a py3 equivalent)
God, I love how awful python can get sometimes!