$statement_type = $hate_ternary ? "if (condition) {} else {}" : $love_ternary ? 'var = condition ? valuetrue : condition2 ? valuefalsetrue : valuefalse' : 'keep = it ? simple : stupid';
Here are my personal guidelines:
Rule 1 - keep ternary operations simple
Rule 2 - use the best format or method
Rule 3 - if you really have to combine tenaries, it's better to daisychain them than to nest them (hint - complicate the False term, not the True term)
Combining ternaries
| Not OK: | my $value = $condition1 ? ($condition2 ? $value1 : $value2) : $value3; |
| OK for some programmers: | my $value = $condition1 ? $value1 : $condition2 ? $value2 : $value3; |
That last example is equivalent to a basic if, elsif, else. This should only be done for basic and simple tasks.
In reply to Re: Ternary operators: a hinderance, not a help
by BitDreamer
in thread Ternary operators: a hinderance, not a help
by Tanalis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |