Code for human not machines
Code is clean if anyone in team can understand, easy to read and enhance by other developer instead of origin author.
Code clean is readability, flexibility, extensibility, maintainability.
Bullet points:
- Keep code simple stupid.
- Reduce complexity algorithm as muc as possible.
- Return code early to keep
code_indent <= 3
- Avoid negative condition
- Follow one consistence coding standard for team.
- Naming convention
- Variable/function/class/module name is meaningful, explanatory.
- Name can be long if it is public, short name is acceptable if it is private.
- Function/ Method name is Verb, meanwhile variable/class/module is Noun.
- Extract magic number to constants.
- Don't repeat yourself: No duplicate code.
- Single responsibility
- Single module/package contains a set of similar classes, that represents one campaign concept.
- Single class wraps a set of similar functions, that represents one duty of campaign.
- Single function covers one and only one purpose, and do it well.
- No god class, long method, many arguments/parameters in method
- Class:
line_of_code < 1000
- Method:
line_of_code < 50
- Method parameters:
total_parameter < 5
- Class:
- Catching and logging exception even if exception is thrown in silent.
- Loose coupling: each software layer (presentation layer/business layer/persistence layer/database layer, etc.) or each part in MVC communicates to each other via abstract interface instead of concreate.
- Code modular: seprate functionality into independent and interchangeble module. Use more
composite/component
pattern instead ofinheritance
.