Tips for Writing Clean Code

Daniel Cook
3 min readMar 18, 2020
Photo by Adli Wahid on Unsplash

Let me preface this with saying these tips are an accumulation of knowledge from working as a web developer for 5 years and from advice derived from various sources, some of which from the famous Clean Code book.

Before starting I think it is necessary to make a general statement about what makes code clean. I believe clean code to be code which is to the point, expresses its intent with minimal confusion and is a pleasure to work with and not a pain. If you’ve worked as a developer you know this is definitely not always the case, particularly when working with third party code. Working with code such as this can be a vital learning experience, as you can see what not to do.

Name Things Expressively but Succinctly

Nobody wants to read a variable with a generic name such as x or y. Name variables and functions so that they describe accurately the data that they hold or the operation which they perform. Also try to avoid very long names, a function with the name getShoppingCartAndCalculateTotal takes a while to read. It also suggests the function is doing more than one thing — a point we will get to next.

Write Small Functions That do One Thing

Avoid writing functions which you need to scroll to read. Try to keep them small — below 20 or 30 lines. Functions should do one thing and do it well. Code not written in this way can often be refactored into small, easier to understand and easier to manage functions. A benefit of this when writing unit tests is that less bugs will go unfound. You are more likely to catch more bugs writing unit tests for smaller functions as each function will have the ability to go wrong in fewer ways.

Write Unit Tests

Having a suite of unit tests allows for you to easily catch bugs when making changes but it also provides a sort of self documentation for the code. You are able to see the expected input and output of functions from the test themselves.

Be sure to write the code in a way which is testable, this likely means different things to different development stacks but generally this means writing small functions and using dependency injection.

Avoid Nested if Statements

Nested if statements can make code difficult to read. It is very difficult visually to keep a track of the nesting of code when it is surrounded in several groups of brackets. This is generally when IDEs tend to try to help out with this with guidelines. This may sound trivial but it invites human error. Try to replace this nesting with conditional early returns where possible. This will be much easier to read and manage.

Do Not Repeat Yourself

Try to write code which is reusable where possible. For example if you are writing CSS, style things such as buttons or forms as general components so this styling does not need to be repeated for different areas across the site. This will make less work for yourself, improve the performance of the site and provide more consistency for the look of the site. It also makes things easier to maintain and makes sweeping changes possible.

--

--