I recently read the book “How to write clean code” by Robert C Martin and it really impacted me in changing the way I wrote an algorithm in general so I thought it would be a good reference to write down the main points which stuck with me when I finished reading it. The book made me realize the errors in my way in structuring a basic method call even, as good code is a reflection of your programming.
Writing good code almost feels like telling a story to your readers, it’s important that they understand what you’re trying to express through your code. So here are the key takeaways from the book:
1. Arguments should not be more than 2
2. A function must carry out only its own task and no secondary task
3. The function length should not be more than 10–20 lines
4. Avoid duplicating the action the function is performing i.e. keep it atomic
5. It should often be a verb or verb phrase name
Code is like humor, if you have to explain it, it’s “bad”
- If you have to use extensive comments, then you don’t understand your own code
2. Comments are used when a program fails to explain what it’s doing and that defeats the purpose of writing it.
3. A clean code doesn’t require commenting unless it’s REGEX
Class and Variable Names
1. The variable names should be related to the task it’s doing
2. Don’t write casual names as an inside joke or a long rambling name which is difficult to edit out later.
3. Use pronounceable names
- “How to write clean code” by Robert C Martin