30 / 06 / 2022

37. why we need and how to start writing clean code 2

Another way to practice clean code approach is to write better functions. First of all, we have to keep it small. This is because shorter function means less things work in a function and makes it to have fewer side effects. Pure function is what we have to achieve. It means one function only lead to a single effect, no matter how many times we run it. To check if the function is "pure" enough, we could try to extract another function from it and rename it. This is the most important point in getting to clean code approach and be able to make a codebase much more easy to maintain and work on.


Another way to make the function more readable is to encapsulate conditionals and avoid naming too much to make it more simple and easy to read. Moreover, we should ensure we have fewer arguments in a function so to make it more readable and easier to write a test on it.

Next, we have to aovid writing flag arguments that means a boolean argument which contradict to the principle of single responsibity. It is better to split them into two separate arguments.


Another important point is to avoid repeating ourselves. This is because when we have to change one thing, the more code we have to edit, the easier to cause bugs if we miss something.

Finally, I am also working towards to make my code as clean as possible. This is hard but it is worthwhile and very important to work on it.