Write clean code… Through dirty code.

Saad El Oulladi
2 min readMar 13, 2020

You might be wondering, how writing ugly code can help you getting your core cleaner.

It’s of course weird to achieve something through the opposite…

Perhaps the most intuitive way to write a good and clean code is to this about the most efficient and writable algorithm. and once it’s all clear in your mind, then you start coding. fair enough ?

This might seem good in theory. However, in reality it’s difficult and energy consuming to plan in advance all the algorithm details before starting code.

Assuming you did plan everything in advance. nothing guarantee that there is no better way to do it. which you might figure out once you finished the implementation.

Note also that finding issues in your code when you test it all at once is very difficult. fixing issues could take more time than the implementation took in the first place.

Personally i use and other approach. At the beginning, i make sure to write something that works. no matter how dirty the code is. only one thing is important. it works !

Once it’s the case. i start reviewing my own code. and improving it little by little.

And after each modification, i test that the code is still working, and nothing has been broken.

This way, fixing issues become is very easy since you know that it’s your last modification that caused it.

And you continue your improvement and testing cycle until there is nothing to be improved in your code.

At this point, you will end up with a code so clean and structured, which was too difficult to plan all at once on the beginning.

This also doesn’t mean to write bad quality code on purpose. The idea here is not to focus on making your code perfect on the beginning. try to do something simple, that works on the first place. and then start improving it through a refactoring cycle.

Important note

Of course this approach doesn’t apply to the main architecture of your app. Since if you base a big part of your app on a random architecture , it will be too late to rework all what you did.

I’m not telling your to drop architectures and design patterns :D

This method only applies to small features and algorithms you add to your code base.

Conclusion.

This is how i manage to write clean code. using ugly code. Of course this method will take more time, since you rethink your logic multiple times. but for sure the time invested is worth it.

--

--