There’s No Such Thing As Perfect Code

You’ve come here on a quest to learn to write perfect code, right? 😉 It’s noble and perfectly reasonable at first blush. It was my quest for a time.

Unfortunately, and fortunately, there’s no such thing as perfect code. We may wish this or that bit of code was better. Others will likely find only some of what we’ve written as intuitive or obvious as we do. We’ll likely find errors even after putting our code into production. For the perfectionists out there, we may never find respite without a healthy dose of self-deceit or narcissism. But don’t take that path! It won’t make you better or help anyone else. 😉

The fortunate thing about the fact there’s no perfect code is we can “stop perfect from being the enemy of good” (Voltaire). It can be incredibly liberating to realize:

  • People think differently, so it’s unlikely everyone will find your code as easy to understand as you do.
  • Software is part science and part art. While some science may have absolute correctness, art is much more subjective. (Also see exact vs. inexact science)
  • Programming computers is a complex endeavor. Humans are prone to mistakes when they engage in such tasks. Don’t be afraid of making mistakes. Identify and fix them to improve your code and skill. You’ll make fewer mistakes as you get better.
  • Life is full of decisions with pros and cons. There’s no perfect car, an ideal place to live, and no perfect computer. We’re constantly selecting things based on the best match for our needs. Design decisions about your software works the same way, so go easy on yourself if you could be better.

Some people seem relaxed about writing perfect code. Creating something that works most of the time or as quickly as possible has its place and merit. Some programmers want to write code that runs at blazing speeds all the time. Others want to write code nobody else can understand because they think it provides job security. But wanting to write code you and others can understand and modify quickly is a noble aspiration to make you a high-value asset to almost any team.

We work with people who rightly focus on time-to-market and want things done quickly. Spending more time writing good source code can accomplish this goal with unexpected success in the long run. See my post on Speed Readable Code for reasons why this works.

I once worked on a project written primarily by someone who boasted they wrote code so nobody could understand it. The code was good in many respects. It provided an effective solution to a complex and relevant problem. It mostly worked. But few people could understand it. We were able to turn the code into something much easier to understand, fix, and extend by lightly refactoring it using many of the techniques we’ll talk about on this site. While there are many subjective aspects of software quality we can debate to no end, many effective ways exist to increase software quality. We’ll revisit this story in future posts.

I bet you don’t fall into a messy coder camp. You want to write perfect code that’s easy to understand, has a high degree of correctness, and will be easy to extend in the future. I wish there were more people like that in the industry, but I am also thankful for people that are experts in other aspects of software development. We need people who think and excel at different things, using varied approaches to produce unique creations. I’m excited to share some ideas that have helped me write better code.