Strive to be boring: How to write better code
In the fast moving world of software engineering, there’s a temptation to write clever, intricate code. Innovation is (and should be!) celebrated, and elegance in code can sometimes overshadow the importance of reliability. However, as I recently learned from an unexpected health scare, simplicity and predictability have immense value—not just in medicine, but in software engineering too.
The appendicitis wake-up call: Predictability saves lives
A few months ago, I found myself in the King's College Hospital's emergency room on a Saturday night. Great start. After a long wait to be triaged, I found out that I had acute appendicitis and would be receiving a laparoscopic appendectomy - which is essentially key-hole surgery to remove the most useless organ we have - the appendix. The surgery itself was routine, but what struck me was the predictability and standardisation of the post-operative care. Every nurse and doctor followed a well-defined, standard procedure. There were no surprises—just established protocols designed to ensure my safety and recovery. This meant that no matter who I saw throughout my recovery, everyone knew exactly what they were looking at and for. This, for example, made my follow up appointment a total breeze - I went to a local doctor's surgery near my house, was seen by a nurse who knew exactly what they were looking at for the stage of recovery I was in.
For me, this experience drew a clear parallel between medicine and software development. Just as medical professionals rely on predictable procedures to ensure patient safety, software engineers should embrace "boring code" to create reliable, maintainable, and secure software.
What is 'boring code'?
"Boring code" might sound unappealing, but it represents a foundational philosophy in software engineering. Boring code is simple, predictable, and understandable by anyone who reads it. It doesn’t rely on clever tricks, obscure libraries, or esoteric design patterns. Instead, it prioritises clarity, maintainability, and ease of use.
In essence, boring code works. It’s code that your team can pick up, understand, and modify with minimal effort. It doesn’t leave colleagues puzzled or diving into Stack Overflow for answers.
As an example, say we wanted to write a function that takes an array of animal names and then outputs which animals in this array are natural predators. Although this at its core is simple to implement, there are two very different paths we can take to get there.
🤓 Complex:
type: embedded-entry-inline id: 52NCg41AsGwMptDznHDw36
🥱 Boring:
type: embedded-entry-inline id: 42fMmhOdVOQdiHJBrU5XjX
What differences do you notice? There are some important themes we should pick up on in the examples above.
Maintainability
The second implementation is more maintainable due to its clear structure and separation of concerns. The first implementation is less maintainable because it’s harder to read and more difficult to extend.
Testability
The second implementation is easier to test and debug. The first one, while concise, could be more challenging to test thoroughly.
Scalability
The second implementation is more scalable, allowing for future growth and changes in requirements. The first implementation may need significant refactoring as complexity increases.
While the first implementation is more concise, the second implementation is superior in terms of maintainability, testability, and scalability, making it the better choice for long-term projects or situations where the function might evolve.
Why boring code matters
1. Maintainability over cleverness
One of the biggest advantages of boring code is maintainability. Software is a team effort, and your code will eventually be maintained by someone else—possibly years down the line. When that time comes, clear, well-structured, and commented code will be invaluable.
Imagine being tasked with updating a piece of code written by a former colleague. Would you prefer navigating through nested ternary operators and lambda functions, or working with clear, straightforward code? The choice is obvious.
2. Reduced risk of bugs
Boring code tends to have fewer bugs because it avoids unnecessary complexity. Complex code introduces edge cases that are difficult to anticipate and test. In contrast, simple, predictable code paths are easier to test, reason about, and debug.
Just as my medical team followed a predictable treatment path during my appendectomy, boring code follows predictable patterns that reduce the risk of unexpected behaviour.
3. Easier onboarding for new developers
New developers joining your team face the challenge of understanding the existing codebase. Boring code makes this process smoother and less intimidating. If your code is clear and consistent, new team members can get up to speed more quickly and contribute to the project with confidence.
4. Faster development cycles
Writing boring code doesn’t slow you down—it speeds you up. By adhering to established conventions and avoiding unnecessary complexity, you reduce the cognitive load on yourself and your team. This means fewer bugs, fewer rewrites, and more time delivering value to your users.
5. Predictability in production
In production environments, predictability is crucial. Boring code, with its simplicity and adherence to established patterns, is more likely to behave as expected. It’s easier to monitor, scale, and troubleshoot. Just as my medical team followed predictable protocols to ensure my recovery, boring code follows predictable paths to ensure system stability.
The pitfalls of 'clever code'
To further highlight the importance of boring code, let’s explore the pitfalls of clever code.
Clever code often involves using advanced language features in ways that are difficult to understand without extensive knowledge or experience. It might solve a problem in a novel way, but at what cost? Clever code can be difficult to read, hard to test, and nearly impossible to maintain over the long term.
Consider this analogy: imagine a surgeon deciding to try an experimental technique during a routine appendectomy. The technique might work, but it introduces unnecessary risk. The patient’s life is at stake, and the surgeon’s job is to follow best practices that have been proven to work time and time again.
Similarly, in software development, our job is to deliver software that works reliably and consistently, and clever code often undermines that goal.
Embracing boring code: Practical tips
So, how can you embrace the power of boring code in your daily work? Here are a few practical tips:
Follow Established Conventions: Adhering to language-specific idioms and team-wide coding standards ensures your code is predictable and easy to understand.
Prioritise Readability: Write code that is easy to read, even if it means sacrificing brevity. Use descriptive variable names, clear function signatures, and comments where necessary.
Avoid Over-Engineering: Resist the urge to introduce complexity where it’s not needed. Focus on solving the problem at hand in the simplest way possible.
Refactor Regularly: Over time, codebases can accumulate complexity. Regular refactoring helps keep the codebase simple and maintainable.
Write Tests: Tests ensure your code works as expected and encourage you to write code that is easy to test—often leading to simpler, more predictable code.
Learn from the Past: Study the codebases of successful projects. Notice how simplicity and clarity are often at the core of their design. Learn from the mistakes and successes of others.
Conclusion: Boring code is beautiful
As software engineers, we’re often tempted to push boundaries and explore the limits of what we can do with code. But sometimes, the most helpful thing we can do is embrace simplicity and predictability. Just as my NHS medical team relied on well-established procedures to ensure my recovery, we should rely on boring code to ensure the safety, reliability, and maintainability of our software.
Writing boring code isn’t always very glamorous, but it is very effective. It’s not flashy, will never make you a 10x developer, but it will make your job and your colleague's job 10x easier. And in the end, that’s what truly matters when building iterative software with a focus on longevity and maintainability.
By embracing boring code, we’re not just writing software—we’re building a foundation for future success, one simple, predictable line at a time.