Clean code that is easily understood by all team members. Clean code can be read and developed by a developer other than its author. Code readability, maintainability, modifiability, and extensibility are only possible when you properly understand how the code works.
- Follow standard rules such as naming classes and functions, the amount of indentation, etc. (Standard Conventions)
- Reduce the complexity in the code as much as possible. Simplicity is always better. (Law KISS)
- Deliver source code cleaner than when you receive it. (Law Boy Scout)
- Always look for finding problems and bugs in the code. To fix them, identify main and root causes. (Act RCA)
- Variables, constants and all configurable data must be at the highest level of abstraction. (Configurable Data At High Levels)
- Instead of breaking the open-closed rule, use the polymorphism property. (Replace Conditional With Polymorphism)
- (Separate multi-threading code)
- Avoid giving your application developers too much freedom of action. (Prevent Over-Configurability)
- Instead of creating objects (using new), use dependency injection. (Dependency Injection)
- Functions and classes should not have access to other details in the code except what they do. (Law LoD)
- Your functions should be small. (Small)
- Each function should do one thing only. (Do one thing)
- The name you choose for each function should fully describe what it does. (Use descriptive names)
- The best function is the Niladic function (Niladic). Your function should take the fewest arguments possible. (Prefer fewer arguments)
- Your function should not change anything that is outside its scope. (Have no side effects)
- Always try to explain yourself in code. If it's not possible, take your time to write a good comment.
- Don't be redundant (e.g.:
i++; // increment i
). - Don't add obvious noise.
- Don't use closing brace comments (e.g.:
} // end of function
). - Don't comment out code. Just remove.
- Use as explanation of intent.
- Use as clarification of code.
- Use as warning of consequences.
- Each test (it is better) to have just one assertion. (One Assertion Per Test)
- Follow the F.I.R.S.T rules.
- Your tests must run quickly, even if there are 1000 of them. (Fast)
- Your tests should be isolated and independent and nothing, including the operating system, environment variables, other programmer's codes, should interfere with their work. (Isolated/Independent)
- Your tests should be conclusive and repeatable, their variables should not change in different environments. (Repeatable)
- (Self-validating)
- Your tests should be easy to read and understand. (Readability)
- Don't mix error handling and code.
- Use Exceptions instead of returning error codes.
- Don't return null, don't pass null either.
- Throw exceptions with context.