The 5 best practices to write clean code

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”- Martin Fowler

What does it mean “to write a clean code”?

Writing clean code is a skill developed over time that all software developers should work on. It basically means writing a code that is easily readable, maintainable and changeable in the future.

This may seem like a simple task, however it is definitely not easy to write clean code. It takes practice, time and effort. So, why should you even bother? I’ll tell you why.

The consequences of bad coding

A study from Stripe, a technology company, showed that developers spend 42% of their time debugging and dealing with “bad code”, costing companies around the world $85 billion per year.

Source image:

If when you program you strive to get things done fast and do not think about future changes or code readability, you probably need to understand a few topics about how to write clean code and therefore become a better programmer. Remember: you don’t need to put all of these topics in practice from night to day, but try to encorporate these tips in your coding journey.

Every programmer is an author. Having in mind that you’re not programming for only the machine to read it, but also for other developers is already a big step.

1. Clean variables names

This is a basic tip, however still many programmers don’t put it to practice. Name your variables with short, pronounceable and revealing names. Also, try not to use abbreviations.

2. Remove duplicate code

The concept of DRY (Don’t Repeat Yourself) is used by several programmers. Sometimes when a piece of code is being used in numerous times in many places inside your code it may be a good idea to place this block of code inside a function or method. This way, if you ever need to make a small change you only need to change this specific function, and all the rest is affected.

3. Don’t add too much comments

Commenting is an excellent practice in programming, this way other developers can understand your code and spend less time debugging it. However you can’t be redundant when commenting, such as the below example. Also never leave a commented code section.

4. Every function needs to do one (and only) task

Don’t add too much tasks in only one function. Your code will look like tangled wires, causing a huge problem because the flow of logic will be difficult to follow and understand when debugging in the future.

5. Always review your code

In order to improve your skills in programming you need to constantly review your code and look for better and more efficient ways of writing it.

Reviewing your code will drastically change the way you program

Try reading a code you wrote six months ago. Or even a year! You’ll realize that there are lots of things you would have done differently if you were to write the same program nowdays.

6. Bonus: stop copying and pasting code

A final bonus tip is: whatever you need to code, do it yourself first. Only if you’re not able to understand the implementation, or have no idea or to start, you can try and read other people’s code.

Yeah, I get it: it is easier and quicker to just google a function and get the work done properly. By using libraries, for example, we don’t always have to reinvent the wheel, as somebody else already did it for us.

However, you may end up using functions that you don’t realize what they do and how they work behind the scenes, and sometimes it is important that you understand the implementation.

And I’ll tell you more: you don’t always know what is implemented behind the library you’re using. Someone with bad intentions could easily put a script where they get all the information inside your code, for example.

Disclaimer: this is not a “don’t ever use libraries or copy other people’s code” advice, but a “try to do it yourself before googling all the answers”.

Hope you guys liked this post, if you did make sure to give it a 👏. If you have any suggestions, leave it in the comments and I will happily add it to this list.



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



software developer, content creator. in love with coming up with new ideas.