Writing clean code is like being a safe driver or eating more vegetables: It’s something most of us aspire to do, but often have a hard time achieving in practice.
If that sounds like a challenge that applies to you, keep reading for tips on how to write cleaner code, no matter which languages you work with. And, because I know there are already dozens of other guides out there about clean coding, this article focuses on practical tips for cleaner coding that go beyond generic advice like “Indent your lines!” or Include comments!”
Why Clean Coding Is Hard
First, let’s discuss why it’s hard to write cleaner code. You might be inclined to blame lazy coders for unclean code. After all, it’s human nature to be sloppy when we think we can get away with it. But, while mere laziness might be one factor, there are other issues that contribute to the production of unclean code, especially in the context of modern programming.
Consider all of the challenges to writing cleaner code:
- Collaboration: It’s easy enough to code cleanly if all the code in your application is yours and you get to decide what clean code means. But many coders today are contributing to large, complex codebases, requiring them to collaborate with others. Achieving consensus about what clean code looks like, let alone enforcing clean coding rules, is more challenging as more developers work together.
- Legacy code: It’s easy to write clean code if you’re developing a new application from scratch. However, much of the code we write today is part of legacy codebases, which may or may not be very clean by modern standards. Figuring out ways to keep new code clean, while also (ideally) cleaning up dirty legacy code, is a lot of work in this context.
- Complex application architectures: Today’s apps tend to be broken into multiple microservices or other components. In a way, this makes coding easier, because programmers can focus on individual chunks of code instead of having to deal with a single monolithic codebase. But it also makes it harder to enforce the same coding standards across the entire project (especially if certain microservices are maintained by certain coders).
- Continuous development: Gone are the days when you could release a new version of your app once a year and look like you were doing a good job. Today’s developers face enormous pressure from managers and end users to do “continuous development,” meaning they are expected to release updates to their applications on a frequent (albeit not literally continuous) basis. When you’re told that getting code out the door quickly is a key priority, it is not always easy to make sure that code is also cleanly written.
Clean Coding Best Practices
Now that we have a more grounded understanding of why it’s hard to write cleaner code in modern development environments, let’s discuss strategies for addressing those challenges.
1. Adopt a coding style guide.
A coding style guide is a set of rules that define how code should be written. It covers basic mechanics, like when and how to indent code, how to name variables and functions, and so on.
When adopted by a group of developers, a style guide helps ensure that everyone follows the same coding conventions. By extension, it reinforces code consistency and cleanliness, even when you have multiple programmers working on a project.
The common objection to coding style guides is that the rules are usually arbitrary. Just because a style guide says you have to do something in a certain way doesn’t mean that’s actually the only viable (or the best) way to do it.
That’s true: Style rules are usually arbitrary. But defining arbitrary rules so that everyone can follow them is the very point of having a style guide. Be upfront about this reality with your team so that they understand that the point of the style guide is not to make them code certain ways “just because,” but, instead, to help everyone write clean code.
There is no official coding style guide that covers every programming language, but Google offers some coding style guides that are a good place to start if you’re looking for one for your team.
2. Choose verbose code or concise code (but don’t do both).
There are two fundamentally different ways to approach clean coding. One is to try to keep your code as short and concise as possible, in the hope that doing so will make the code easier for others to follow and maintain. It’s for this reason that so many programmers love writing i++.
The other approach is to be verbose, with the idea that verbose code is simpler to understand without having to read between the lines. This is why you might write i = i+1, even though you know that i++ would do the job just as well.
There are arguments for and against each approach. I’m not going to get into that debate here. Instead, I’ll simply recommend that you (and any colleagues working with you) choose one or the other: Being concise in one function and verbose in another leads to inconsistency and uncertainty.
3. Have other programmers read your code.
When you were learning to write in middle school, your teacher probably recommended that you invite others to read your writing to see if they could make sense of it. This exercise helps writers identify prose that might perfect make sense to them but is unintelligible (or at least unclear) to others.
You can use the same strategy to help write clean code. As you write code, ask colleagues to take a look at a function or class you just implemented, or to review the way you’ve structured data. They may have suggestions for making the code cleaner, even if you think it’s perfectly clean.
This basic collaboration might seem like an obvious way to help keep code clean. But the reality is that developers rarely review code written by other people until it has been integrated. And, at that point, it’s harder to revise the code to make it cleaner.
4. Choose a clean programming language.
If you have the luxury of choosing which language to code in, pick a language that promotes clean coding practices.
To be sure, the extent to which a given language is “clean” by its nature is largely subjective. But there are still some languages that are cleaner by default than others--at least, in most people’s opinions. Andrew Vos’ study of curse words within comments in different programming languages is enlightening in this regard; some languages are subject to more curse words than others.
Sure, the number of times programmers use profanity within their comments may not be an exact reflection of whether one language is cleaner than another. However, I suspect that many developers would agree that C++ tends not to be very clean--or, at least, that it’s harder to keep C++ code clean than, say, Python. Vos’ findings would seem to reflect this.
Writing cleaner code is hard, and not just because you’re lazy. And while there are a variety of resources out there designed to help you write clean code, the strategies described above are not often included in them. I hope these tips provide some useful, additional guidance for keeping your code lean, mean and clean.