Jack Franklin

Keeping Code Simple

When I think about keeping code simple, I think about the progression of your average software engineer from junior to mid to senior, and this one commonly mentioned part of that journey:

I've seen this pattern play out multiple times - and I've been each of those engineers. I remember refactoring some code in a pull request that a junior developer on our team wrote, thinking I was so smart. I made so many improvements - and got it down from 10 lines to 4! That's fantastic, I thought. The change got merged into the code base and not long after it was largely reverted back to its original state because people needed to work with that code, and working with such succinct code with so much squashed into just four lines was nearly impossible. I learned a good lesson that day: lines of code are not a good metric of code quality.

I think about this tweet about using reduce by Jake Archibald often:

All code using array.reduce should be rewritten without array.reduce so it's readable by humans

Whether or not you agree about the specifics of the reduce function isn't important, but the sentiment behind Jake's tweet is. You're not writing code to impress your colleagues. The "smartness" of your code doesn't matter. The readability of your code does. Keep code simple and it will be less effort to understand, less effort to change, and less frustrating to work with. Traits of simple code include (but are not limited to) the list below:

If some of those points feel a bit vague, don't worry. It's hard to summarise in one quick list; we will be diving into each of the topics above in a dedicated blog post.

Sometimes, code just cannot be made simple. Maybe you're working with a horrible legacy API whose interface is bizarre in every way possible, or you're stuck on an old version of a library that you can't upgrade for a variety of reasons. Most codebases I've worked on have had a rough edge or a dark corner that developers shy away from. We'll look at techniques to tackle this too and migrate away from dark dingy corners to codebases and modules that are a pleasure to work on.

If you've got any questions, comments, feedback, or just want to say hello, get in touch with me on Twitter.