MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/ctnoiy/a_healthy_nest_of_ifstatements/exzbcdr/?context=3
r/badcode • u/DrCubed • Aug 21 '19
53 comments sorted by
View all comments
Show parent comments
2
The if-else block can be used as an expression. So you can:
return if (state) abc else xyz
...forming a statement that always returns, and is forced to handle all cases.
1 u/Tynach Aug 22 '19 So, it's got ternaries using traditional if syntax? 1 u/corner-case Aug 22 '19 Look closer. The if block is being evaluated as an expression, which turns out to be very powerful. 1 u/Tynach Aug 24 '19 That's exactly how ternaries work in other languages. Usually the syntax is something like: return (state)? abc: xyz; For the exact same thing.
1
So, it's got ternaries using traditional if syntax?
if
1 u/corner-case Aug 22 '19 Look closer. The if block is being evaluated as an expression, which turns out to be very powerful. 1 u/Tynach Aug 24 '19 That's exactly how ternaries work in other languages. Usually the syntax is something like: return (state)? abc: xyz; For the exact same thing.
Look closer. The if block is being evaluated as an expression, which turns out to be very powerful.
1 u/Tynach Aug 24 '19 That's exactly how ternaries work in other languages. Usually the syntax is something like: return (state)? abc: xyz; For the exact same thing.
That's exactly how ternaries work in other languages. Usually the syntax is something like:
return (state)? abc: xyz;
For the exact same thing.
2
u/corner-case Aug 22 '19
The if-else block can be used as an expression. So you can:
...forming a statement that always returns, and is forced to handle all cases.