9 Clean Code principles you can apply NOW in your code

Did you know that maintaining software is as important as building it? But, in order to maintain it, some practices must be implemented in the development team.

Have you ever heard of the Boy Scout rule? You should leave the camp cleaner than you found it. This rule must also be followed by the developer who begins undertaking software maintenance.

As a result, Clean Code is more than a methodology. It's a philosophy that is extensively used in the technology teams of organizations and startups to keep software as bug-free and up to date as possible.

As a result, I wrote this article to explain the core ideas, best practices, and benefits of working with an emphasis on clean code.

Photo by Towfiqu barbhuiya on Unsplash

How was it created?

The book Clean Code: a Handbook of Agile Software Craftsmanship introduced Clean Code and associated techniques. Robert Cecil Martin (also known as Uncle Bob) wrote the book, which was published in 2008.

Martin has been in development since the 1970s. He is one of the 17 signers to the Agile Manifesto, written in 2001 and is a declaration of fundamental ideals and principles for software development (I recommend you read it since it's widely used by companies nowadays).

9 Clean Code Principles

Uncle Bob provides at least 9 good principles for using Clean Code in his book to have your code working smoothly. They are as follows:

1. Meaningful Names:

Names are everywhere in software. We name variables, functions, arguments, classes, files and directories. Because we do so much of it, we’d better do it well. So use names in your code that are intention-revealing, that avoid disinformation, that are pronounceable, searchable and meaningful.

2. Be careful with comments:

Nothing is more beneficial than a well-placed comment. Also, nothing is more destructive than an old crufty comment that spreads lies and disinformation. Therefore, comments aren't always good, and can even be harmful at times. So use comments only when strictly necessary and remember to update them whenever you make changes to the code.

3. Don't Repeat Yourself

One of the most important rules in Clean Code. This concept shows that there should not be two parts of the software performing the same function. Every time you see duplication in the code, it represents a missed opportunity for abstraction (i.e. extracting that code to a separate function or class).

Kent Beck also made it one of the principles of Extreme Programming and called it: “Once, and only once”.

4. Follow code patterns

Patterns make the code much easier to understand. Thus, character variations in naming, such as the usage of capital or lowercase letters (namePerson, NamePerson, nameperson), should be standardized.

If you are continuing a project that has already begun, pay attention to the patterns that have been created and keep them in your updates; this practice improves the performance of other developers and prevents system issues.

5. The Boy Scout Rule

I've already mentioned this but leave everything cleaner than you found it. It’s not enough to write the good code. The code has to be kept clean over time. The code could not become a mess if we all submit our code a little cleaner than when we first saw it.

6. Error Handling

When we program, we must deal with errors. Things can go wrong, and when they do, we are accountable for ensuring that our code performs what it is supposed to do. Then, clean code must readable, but also be robust. It must do what was planned, but if an error appears, it should be able to handle it.

7. Keep tests clean

Code is only considered clean after it passes the tests. However, after the programmers get a suite of tests to pass, they must make sure that those tests were convenient to run for anyone else who needed to work with the code. For example, the test suite shouldn't take too long to execute, because if that happens the team will be discouraged to put the code through tests.

8. Keep functions small

The first rule of functions is that they should be small. Functions should do only one thing and do it well. If you have a function in your code with more one concern (i.e. doing more than one thing), that function can be split in smaller functions with meaningful names. This makes your code easier to understand and maintain in the future.

9. Think of your code as a book

This last principle principle is essential to Clean Code. Your code must be easy to read and understand, like a regular book. Other developers reading your code, must be able to recognize what each function does, what each variable is used for and the general structure of the system.

Conclusion

In general, developers spend more time studying existing code and trying to redo it than writing new code. And, because time is of the essence in agile teams, it's critical to use to best practices in order to have clean code and make everyone’s work easier.

In short, a poorly designed code will meet the requirements, but problems will inevitably arise, particularly when it comes time to maintain or upgrade it.

Thanks for reading! Follow me in this platform to read more development content. Have a great day, see you soon! 👋

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Justin Graysen

👨‍💻 Web Developer 🖋 Tech Content Writer 📚Follow me for Javascript and web dev content daily