4 ms·
Isn't a ternary here nicer? return (foo == 0) ? 0 : bar / foo or even return bar / max(1, foo) in the case where foo is integer or tiny foo would overfl
by smallnamespace 8y ago
Isn't a ternary here nicer?
return (foo == 0) ? 0 : bar / foo
or even
return bar / max(1, foo)
in the case where foo is integer or tiny foo would overflow your range anyway
- KennyCason 8y agoI would say it doesn't really matter much as both are readable. I would prefer either the original if/else or the ternary solution over the last solution as I personally think the intent is not as immediately clear in your "max" form. I would optimize for readability over density, unless there is some reason to write it differently (e.g. performance). just for fun, in kotlin (making use of expressions removes some verbosity): return where(den) { 0 -> 0 else -> num / den } return if (den == 0) { 0 } else { num / den }
- Dylan16807 8y agoTernary doesn't really look any better. I'd do this in a language that supports it though: return foo && bar / foo