Earlier quoted context omitted.
What's wrong with ternary?
Nothing but when you nest them a whole bunch they are far harder to read and understand that if/else.
In fact, I believe the problem with the JS example's readability is the formatting. Here's how I'd format the same code, and I find this quite readable:
function deriv(exp, variable) {
return is_number(exp)
? 0
: is_variable(exp)
? is_same_variable(exp, variable)
? 1
: 0
: is_sum(exp)
? make_sum(
deriv(addend(exp), variable),
deriv(augend(exp), variable)
)
: is_product(exp)
? make_sum(
make_product(multiplier(exp), deriv(multiplicand(exp), variable)),
make_product(deriv(multiplier(exp), variable), multiplicand(exp))
)
: error(exp, "unknown expression type -- deriv");
}