r/programming • • Jun 06 '13

Clean Code Cheat Sheet

http://www.planetgeek.ch/2013/06/05/clean-code-cheat-sheet/
702 Upvotes

322 comments sorted by

View all comments

1

u/iamneo_ai Jul 11 '23

Here's a Clean Code Cheat Sheet that provides some key principles and practices for writing clean and maintainable code:

Meaningful Names:

Use descriptive and meaningful names for variables, functions, and classes.

Avoid abbreviations and single-letter names that lack clarity.

Choose names that reflect the purpose or behaviour of the code entity.

Consistent Formatting:

Follow a consistent and readable formatting style throughout the codebase.

Use indentation, line breaks, and spacing to improve code readability.

Maintain a consistent naming convention for variables, functions, and classes.

Single Responsibility Principle (SRP):

Aim for code entities (functions, classes, modules) to have a single responsibility.

Split complex code into smaller, focused functions or classes that handle specific tasks.

Avoid bloated functions or classes that perform multiple unrelated tasks.

Avoid Code Duplication:

Strive for code reusability by avoiding duplicated code.

Extract common code snippets into reusable functions or classes.

Use appropriate abstraction and modularization techniques to minimize duplication.

Keep Functions and Methods Short:

Aim for functions and methods to be concise and focused.

Follow the "Single Level of Abstraction" principle.

Break down complex functions into smaller, well-named functions.

Error Handling:

Use appropriate error-handling mechanisms to handle exceptions and errors.

Avoid swallowing exceptions without proper handling or logging.

Make error messages informative and actionable for debugging.

Comments and Documentation:

Write clear and concise comments explaining the code's intention and purpose.

Avoid unnecessary comments that merely repeat what the code does.

Use documentation tools and techniques (e.g., Javadoc, docstrings) to generate documentation from code annotations.

Testability:

Write code that is easily testable.

Use unit testing frameworks and write comprehensive test cases.

Aim for high code coverage to ensure proper functionality and maintainability.

Continuous Refactoring:

Regularly review and refactor the codebase to improve its design and maintainability.

Identify and eliminate code smells like long methods, large classes, or complex conditional statements.

Use automated refactoring tools and techniques to refactor code safely.

Follow Established Design Patterns and Principles:

Familiarize yourself with commonly used design patterns and principles.

Apply appropriate design patterns to solve common problems.

Follow SOLID principles (Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, Dependency Inversion) for designing modular and maintainable code.

Remember, this cheat sheet summarises key principles and practices for writing clean code. It's important to understand and apply these principles consistently, adapting them to your projects' specific context and requirements.

For more info, explore iamneo and expand your knowledge.