- Use Meaningful Names: Names should reveal intent. Choose names that describe the purpose or role of the variable, method, or class.
- Avoid Encodings: Do not include type or scope information in the name (e.g., Hungarian notation (public static int publicStaticIntPrice=10)).
- Use Pronounceable Names: This makes it easier to discuss the code.
- Use Searchable Names: Avoid using single letters or overly short names, except for temporary variables.
- Consistency: Use a consistent naming convention throughout the codebase (e.g., camelCase for variables and methods, PascalCase for classes).
- Small Functions: Functions should be small and do one and only one thing.
- Single Responsibility Principle: Each function should have only one responsiblity.
- Descriptive Names: Function names should clearly describe what they do.
- Avoid Side Effects: Functions should avoid altering the state of the system or causing unexpected behavior.
- Limit the Number of Arguments: Prefer fewer arguments; consider using objects if more than three arguments are needed.
- Self-Explanatory Code: Write code that is self-explanatory so that comments are not needed.
- Useful Comments: Use comments to explain why something is done, not what is done.
- Avoid Redundant Comments: Avoid comments that restate the obvious or duplicate the code.
- Update Comments: Ensure comments are updated when the code changes.
- Consistent Indentation: Use consistent indentation to improve readability.
- Line Length: Limit line length to a reasonable amount (typically 80-120 characters).
- Whitespace: Use whitespace to separate logical sections of code and improve readability.
- Blocks: Use curly braces for blocks, even if optional, to avoid errors and improve readability.
- Use Exceptions: Prefer exceptions over error codes for error handling.
- Catch Specific Exceptions: Catch specific exceptions rather than using a general catch-all.
- Fail Fast: Detect errors early and handle them as close to the source as possible.
- Provide Context: Include meaningful messages and context with exceptions to aid debugging.
- Small Classes: Classes should be small and focused on a single responsibility.
- Encapsulation: Keep internal details private and expose only what is necessary.
- Cohesion: Ensure that class members are related and work together towards a common goal.
- Dependency Injection: Use dependency injection to decouple classes and make them more testable.
- DRY (Don't Repeat Yourself): Avoid code duplication by abstracting common functionality.
- KISS (Keep It Simple, Stupid): Keep the code as simple as possible.
- YAGNI (You Aren't Gonna Need It): Don't add functionality until it is necessary.
- SOLID Principles: Follow SOLID principles for object-oriented design:
- S: Single Responsibility Principle
- O: Open/Closed Principle
- L: Liskov Substitution Principle
- I: Interface Segregation Principle
- D: Dependency Inversion Principle
- Write Tests: Ensure code is covered by automated tests.
- Test Coverage: Aim for high test coverage but prioritize meaningful tests over coverage percentage.
- Isolate Tests: Tests should be isolated and independent of each other.
- Readable Tests: Write tests that are easy to read and understand.
- Special thanks to Robert C. Martin (Uncle Bob) for introduction of Clean Code in very easy way.