3 ms·
This is one of those classic statements that you only understand correctly after you have the experience it intends to impart. It teaches you nothing by itself.
by munificent 10y ago
This is one of those classic statements that you only understand correctly after you have the experience it intends to impart. It teaches you nothing by itself. To really understand it, you need a clearer understanding of "premature" and "optimization". (Keeping track of the preceding text that clarifies that the quote is about small efficiencies doesn't hurt either.)
If a performance choice affects the overall architecture, a public API, the user experience, or the success of your product in the market, optimizing now is not "premature". Now is exactly the time to get it right because if you don't, there might not be a later time to optimize. It is only premature if it taking the slow path doesn't matter much today and the technical debt won't accrue quickly such that it really matters in the future.
If you can safely optimize it tomorrow, without it being significantly more costly to do so than it would be today, it's probably premature. Otherwise, it's probably not.
Likewise, "optimization" doesn't just mean any code where you think about performance at all. You should always be aware of the performance of your code, just like you're aware of its maintainability, robustness, and test coverage. Performance is a fundamental aspect of the craft.
Optimization is really about improving the performance of the code at the expense of other valuable things. Optimized code either takes significantly longer to develop (burning time you could have spent on other features or robustness), or is significantly harder to maintain (usually because the optimization relies on certain assumptions that you can no longer be flexible about) than non-optimized code.
If the faster version doesn't take much longer to develop and doesn't lead to a more calcified codebase, then doing that isn't optimization. Not doing is just silly.