Before implementing an optimization:
- measure
- profile
- analyze the negative sides of the optimization
The fact that an operation is ran redundantly doesn't necessarily mean it's a problem, it becomes a problem when there's a notable cost in time or resources.
Therefore measure first to make sure that there really is a problem. And measure several times to negate the effects of possible caching, network latency etc.
Profiler
Programmers often err in guessing where the problems in a program are which is why it's paramount to use a profiler to find about problems.
So use a profiler to find out where the actual problem is. Verify the problem area by doing a quick optimization. If even a quick one is hard to implement, temporarily comment out the problem functionality, and then measure again to make sure that optimizing it will actually do something good.
Considerations
If an optimization can be implemented, estimate:
- how long will the proper optimization take
- how much it will break things
- how it will affect maintainability
- how it will affect the rest of the system
In addition, if the optimization actually improves maintainability it should be considered good design which also happens to optimize.
There are alternatives to optimizing such as
- altering the functionality
- removing it altogether
- letting the functionality be a resource hog
If a profiler is too hard to get, use low tech means like a stopwatch and ending the program at certain points to get measures.
After deploying there's the risk that the optimization doesn't actually fix the problem, and it should be verified by measuring again after deploying.
Recheck
Optimizations often depend on the inner workings of other code and libraries, so they often perform less effectively or even break after updates. To counteract these, optimizations should be tested and measured again after updates. Therefore optimizations should be documented.
Conclusion
Overall, optimizing shouldn't be taken lightly since the program loses a part of its maintainability, and the programmer, management and QA need to spend time on the issue.
More information
Online
Literature
- Code Complete (Chapters 24 and 25) by Steve McConnell
- Effective Java, 2nd edition (Item 55: Optimize judiciously) by Joshua Bloch