dagblog.
← Archive

Code optimization and development efficiency


When I touched on this in a recent post about unoptimized code, I suspected some developers would dust off the familiar line “Does it work? Then leave it alone.” That’s more or less the first rule of optimization, “Don’t optimize!” So when should you optimize, and when should you focus on getting development done efficiently?

Unoptimized code is an extreme in one direction. At the other extreme, code can be so thoroughly optimized that even its author can’t understand it five minutes after writing it. Code swings between these extremes. Many factors can push it one way or the other, but they aren’t the focus here.

The real art of writing code lies in encapsulating it in objects instead of trying to make it as optimal as possible. Encapsulation lets us replace parts of the code without changing what surrounds them. Say we need to speed up an array sort. We simply swap out the object that implements the sorting algorithm. Encapsulation makes optimization non-invasive. The new algorithm leaves the surrounding code alone.

Development stays efficient because optimization amounts to swapping one object for another, which we can do anytime. That makes the question of when to optimize and when to focus on development efficiency easy to answer. Efficient development and optimization are compatible and can reinforce each other. You can always fit optimization into an efficient development process.

Medium version

Related articles

Related CZ Podcast episodes