- Write code for people, not machines: Your code should be easy for other programmers to read, understand, and modify. Always strive to write code that is clear, concise, and easy to follow.
- Keep functions and classes small: Functions should be short and focused on a single task, and classes should be small and well-defined. This makes your code easier to understand and maintain.
- Use descriptive names: Choose descriptive names for variables, functions, and classes that accurately reflect their purpose and meaning.
- Avoid duplicating code: Duplication is a breeding ground for bugs and can make your code harder to maintain. Use functions, classes, and libraries to reuse code as much as possible.
- Write tests for your code: Automated tests help you catch bugs early, provide documentation for your code, and make it easier to refactor your code later.
- Keep your code simple: Simplicity is key to writing clean code. Avoid unnecessary complexity and use the simplest solution that meets your requirements.
- Keep your code DRY (Don’t Repeat Yourself): DRY is a principle that promotes code reuse and can help you avoid duplication. If you find yourself repeating the same code in multiple places, it’s probably time to refactor.
- Follow the Single Responsibility Principle (SRP): The SRP states that each class or function should have only one responsibility. This makes your code easier to understand, test, and maintain.
- Write code that is easy to change: You can’t predict all the changes that your code will need to accommodate in the future, so make your code as flexible and adaptable as possible.
- Continuously improve your code: Clean code is not a one-time effort, but an ongoing process. Continuously look for ways to improve your code, and be open to feedback and suggestions from others.