DEV Community

Discussion on: Why I'm phasing out ternary statements

Collapse
 
holywar20 profile image
Bryan Winter • Edited

I have a controversial opinion on this ... I'm not sure this is something worth really considering in 99.9% of use cases.

This can easily lead one to premature optimization. Most code executes rarely, and consumes fractions of a fraction of resources. Optimizing a millionth of a second benefit on code that executes say once per session is probably not a good use of your cognitive resources.

In fact, I'd argue that even if IF or Tenary was 100x more efficient than the other ( and it doesn't appear to be ), that it's generally bad to write code 'optimally performant' code.

Optimal performance can easily be done by building applications entirely in assembly and without using frameworks or high level languages at all. But the reason we write almost nothing in assembly is because the biggest barrier to getting a functioning application is the human writing it. Generally if a coder is writing an IF statement, your doing a high-level check, which compared to all the billions of things a computer does a second, isn't all that much time, and you would need to write a billion lines of code to get a few seconds of extra performance across all your applications built over your lifetime.

Optimize for readability first - add performance improvements later after you have had a chance to actually profile the code and figure out where your bottlenecks are.