I’m a fan of optimization. My coding process consists of three parts: designing new features, testing, and optimizing. In my previous article, I focused on how optimization can reduce energy costs. After receiving several messages about optimization, I decided to describe the most common mistakes that occur during optimization. Let’s take a look at them today.
1. Optimizing before testing
- Testing should precede optimization. Before diving into optimizing your code, ensure that everything works correctly according to the design and requirements. There’s no point in optimizing a broken feature.
2. Relying solely on intuition
- If you don’t have profiling tools like cProfile in your toolkit, add them. Optimizing based on intuition is not a good idea because you end up optimizing assumptions instead of actual performance.
3. DRY vs. WET
- Two popular programming paradigms, DRY (Don’t Repeat Yourself) and WET (Write Everything Twice), require consistency. Choose one and stick to it. Personally, I prefer WET, so if something appears in the code more than twice, I focus on optimization.
4. Inefficient algorithms
- Inefficient algorithms can lead to trouble. Many developers today lack a strong foundation in algorithm design, but the truth is that algorithms make tasks easier. If you know only one sorting algorithm, invest more time in learning new approaches.
5. Overlooking readability
- This often happens with excessive optimization. Too much of a good thing can be harmful, and optimization is no exception. I’ve frequently seen comments disappear or code blocks become less readable than the original. This should never happen—optimization should improve, not degrade, the code.
6. Ignoring memory usage
- Faster code isn’t enough if it’s at the expense of memory. Optimization that sacrifices memory efficiency is not done correctly.
7. Neglecting tests
- Tests often face skepticism. I’m not an advocate for 100% code coverage. Personally, I think it’s not feasible. However, after refactoring, updating or writing new tests should follow.
8. Focusing on minor details
- This is my least favorite issue, which I’ve encountered many times in practice. Optimizing minor details while neglecting the system as a whole.
9. Forgetting about caching
- Caching is one of my favorite strategies—perhaps because it’s where my love for optimization began. If you’re repeatedly performing the same computation, it’s time to embrace caching strategies.
10. Missing documentation
- Sometimes, documentation feels like a mythical creature in programming. Everyone talks about it, everyone knows about it, but almost no one has seen it. Please, love documentation. Document your changes. It helps not only your colleagues understand the system but also juniors who appreciate every bit of guidance. Lastly, it will help you when you revisit your work after a long time.
Conclusion
Optimization is, to some extent, an art of balancing performance, sustainability, and code readability. Avoiding the most common mistakes will make optimization your best friend, as it has become mine. The great thing about optimization is that you can never be a champion—you always have the opportunity to learn something new about it.
Leave a Reply