Why and how to write clean code
Cleaning code is just a waste of time. My code is working well. The server is up and running perfectly. The website or app looks great. The client is happy and my job is done! So, why should I worry about cleaning my code? This is the question being asked by many people whenever someone advises cleaning the code. If anyone is thinking similarly then you should read this.
I was always arguing with friends why anyone should spend more time cleaning the junk in code rather than building/learning something new. Our goal is to build a product or solve a particular problem. It is good if it is done quickly and if that works.
I understood the real importance of clean code only after working on a project, developed and managed by many people. Initially, I thought, I would need just one-two months to understand and get hands-on on the project. But I was proved wrong.
There were thousands of lines of code in many files, with hundreds of lines being in a single function. I could see duplicated copies of code in every function that I visited. The nesting of function was too deep and every smaller piece of function was being referred to in many larger functions, resulting in a hell of a lot of new bugs while developing or fixing a bug. Many unnecessary comments on unused code confused me several times for making any change. At a point, it became impossible to add any functionality. For once I thought, I would rather start writing the code from scratch than making changes in existing code. Only then did I want to write Clean Code so that no one blames me for the cause.
There is a saying “By others' mistakes, the wise correct their own”. I am sharing my understandings here.
Why clean code
- I want to introduce fewer or zero bugs when I modify(add/update/delete) any piece of code.
- I should be able to start working quickly and confidently when I come back to the same code after months/years.
- It should be easy for anyone else to touch my code and fix any bugs in my absence.
- I do not want to write a code that becomes impossible to scale/maintain at a certain point in time and breaks the product.
How do I write clean code?
Here are some clean code principles that anyone can easily follow in everyday development.
- DRY (Don’t Repeat Yourself) — Immediately stop if you think you are writing the same code which already exists. Every piece of code should have a single responsibility and be unambiguous.
- KIS (Keep It Simple) — Ask yourself: Can this be made simpler? Always keep the functions simpler and avoid complexities in the code as much as possible.
- COMMENT — Don’t over comment but add necessary comments when logic gets harder to understand.
- ARCHITECTURE — Use any architecture principle that fits best to the project like MVC, MVP, MVVM, etc.,
- DESIGN PATTERNS — Make use of existing best suitable design patterns for any problem as there are already tons of available design patterns for our problems.
- CONVENTIONS — Follow the same coding or naming convention across the team.
- REFACTOR — Make this a habit. Refactor code ASAP and remove any code or comments if you do not need it anymore.
- REUSE — Make use of already existing libraries or functions. Those will be much more efficient and faster than our code.
- SOLID PRINCIPLES — Please follow Object-Oriented Design(OOD) principles.
What do I get after writing clean code?
You will feel very confident and happy to share your code with anyone, anytime. Your code will be more readable and easily maintainable by you or anyone else in your absence. You will have to put very little effort or zero effort sometimes to make someone understand your code. It’s not how we make mistakes, but how we correct them that defines us.
Please do read the book Clean Code by our uncle Bob, Robert C.Martin. Check this Summary of ‘Clean code’ by Robert C. Martin for quick reference.