I wonder: “when is ternary operator worse than just using ‘if-else’ block?”
I suppose, it could be with debug-time (e.g. ternary operator is bad for debugging)
Is such beauty useful? Maybe there are some situations when simple if-else block is better for using?
thank you
A ternary operator is useful when you want to conditionally evaluate one of two simple expressions and do something simple with the result (e.g. assign it to a variable or return it). Something like this is an acceptable use of the ternary operator:
If your code is more complicated than this then it is usually clearer to use an if statement otherwise you risk trying to do too much in a single line of code. It’s not just about ease of debugging – a long line with a complex expression can be difficult to understand.
In many languages it is not allowed to use statements (as opposed to expressions) in a
? :expression. If you need to execute a series of statements then you have no choice except to use anifstatement.