Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.
- Follow standard conventions.
- Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
- Boy scout rule. Leave the campground cleaner than you found it.
- Always find root cause. Always look for the root cause of a problem.
- Keep configurable data at high levels.
- Prefer polymorphism to if/else or switch/case.
- Separate multi-threading code.
- Prevent over-configurability.
- Use dependency injection.
- Follow Law of Demeter. A class should know only its direct dependencies.
- Be consistent. If you do something a certain way, do all similar things in the same way.
- Use explanatory variables.
- Encapsulate boundary conditions. Boundary conditions are hard to keep track of. Put the processing for them in one place.
- Prefer dedicated value objects to primitive type.
- Avoid logical dependency. Don't write methods which works correctly depending on something else in the same class.
- Avoid negative conditionals.
- Choose descriptive and unambiguous names.
- They are a powerful tool, use them communicate with others.
- Make meaningful distinction.
- Always communicate your intent, if you have to put a comment, your name is a bad one.
- Avoid disinformation
- A name must says what it means, and means what it says
- Remember, people will have to talk about your code
- Use pronounceable names
- If you have to read the code to understand the meaning, it's a bad name.
- Avoid encoding.
- Encodings are distracting. They obfuscate the code. They make it hard to read
- No Hungarian Notation, "m_", "p_"
- Clean code should always read like well-written prose
- Classes: noun
- Variables: nouns
- Methods: Verb names. If they're Booleans, make them predicates.
- Make sure that when you write a line of code, it looks like and reads like a sentence.
- Scope rule
- Variable names: short, even one letter, if they're in a tiny little scope, but variable names should be long if they're in a big long scope.
- Function names: short if they've got a long scope. Long and descriptive, if they have a short scope.
- Classes names. Short names for public classes. Longer names for private classes in tiny scopes.