“Master programmers think of systems as stories to be told rather than programs to be written” — Uncle Bob.
Main Characteristics of Clean Code
- Elegant: Clean code is pleasing to read, should make you smile.
- Readability: Clean code should read like well-written prose.
- Simple: Do one thing with the Single Responsibility Principle (SRP).
- Testable: Run all the tests.
Names Rules
- Use intention-revealing Names
- Make meaningful distinction
- Use Pronounceable Names
- Use searchable names
- Pick One Word per Concept
- Avoid Disinformation
- Avoid Encodings
- Avoid Mental Mapping
- Don’t Pun
- Classes and objects should have noun or noun phrase names
- Methods should have a verb or verb phrase names
Functions Rules
- Small
- Do one thing
- Use descriptive names
- Prefer fewer arguments
- Don’t Repeat Yourself
- Have no side effects
Comments Rules
- Always try to explain yourself in code.
- Don’t be redundant.
- Don’t add obvious noise.
- Don’t use closing brace comments.
- Don’t comment out code. Just remove.
- Use as explanation of intent.
- Use as clarification of code.
- Use as warning of consequences.
Unit Tests
Rules
- One assert per test.
- Readable.
- Fast.
- Independent.
- Repeatable.
Test Driven Development Laws
- First Law: You may not write production code until you have written a failing unit test.
- Second Law: You may not write more of a unit test than is sufficient to fail, and not compiling is failing.
- Third Law: You may not write more production code than is sufficient to pass the currently failing test.
Code Smells
- Rigidity. The software is difficult to change. A small change causes a cascade of subsequent changes.
- Fragility. The software breaks in many places due to a single change.
- Immobility. You cannot reuse parts of the code in other projects because of involved risks and high effort.
- Needless Complexity.
- Needless Repetition.
- Opacity. The code is hard to understand.
Reference
Summary of ‘Clean code’ by Robert C. Martin
How to write Clean Code?