HomeContact
Enhancement
Clean Code: A Comprehensive Checklist
Syihab Muchsin
August 14, 2024
3 min

Clean Code: A Comprehensive Checklist

Writing clean code is an essential skill that every developer should master. Clean code is not just about making your code functional; it’s about making it easy to read, maintain, and extend. Here’s a detailed checklist to help you write clean code:

1. Naming

  • Use Descriptive and Meaningful Names:

    • Variables & Properties: Use nouns or short phrases with adjectives. Examples: orderTotal, customerName.
    • Functions and Methods: Use verbs or short phrases with adjectives. Examples: calculateDiscount, sendEmail.
    • Classes: Use nouns to represent entities or objects. Examples: Invoice, UserAccount.
  • Be as Specific as Necessary and Possible: Choose names that clearly convey their purpose and usage.

  • Use Yes/No “Questions” for Booleans: Name boolean variables as if they answer a yes/no question. Examples: isValid, hasAccess.

  • Avoid Misleading Names: Ensure that names accurately reflect what they represent or do.

  • Be Consistent with Your Names: Stick to consistent naming conventions throughout the codebase. For instance, if you use get for retrieval functions, avoid mixing it with fetch.

2. Comments & Formatting

  • Most Comments Are Bad - Avoid Them! Aim to write code that is self-explanatory, reducing the need for comments.

  • Some Good Comments Are Acceptable:

    • Legal Comments: Necessary for licenses and legal information.
    • Warnings: Important warnings or information that must be heeded.
    • Helpful Explanations: Use comments for complex logic, such as regular expressions.
    • Todos: Use sparingly for tasks that need future attention.
  • Use Vertical Formatting:

    • Keep Related Concepts Close: Ensure that related lines of code are placed close to each other to maintain readability (vertical density).
    • Add Spacing Between Unrelated Concepts: Use blank lines to separate different sections or concepts (vertical distance).
    • Write Code Top to Bottom: Write your code in a top-to-bottom manner, where functions that call others are placed above the ones being called.
  • Use Horizontal Formatting:

    • Avoid Long Lines: Break long lines of code into multiple shorter lines to improve readability.
    • Use Indentation to Express Scope: Clearly indicate code blocks and scope through proper indentation.

3. Functions

  • Limit the Number of Parameters: Functions should ideally have a small number of parameters. If there are too many, consider grouping them into objects, dictionaries, or arrays.

  • Functions Should Be Small and Do One Thing:

    • Levels of Abstraction: The function body should maintain a consistent level of abstraction, one level below that implied by the function name.
    • Avoid Mixing Levels of Abstraction: Keep high-level and low-level operations separate within functions.
    • Avoid Redundant Splitting: Don’t over-split functions unnecessarily, which can lead to confusion.
  • Stay DRY (Don’t Repeat Yourself): Avoid duplicating code; instead, refactor common code into functions or classes.

  • Avoid Unexpected Side Effects: Functions should not produce side effects that aren’t apparent from their names or input parameters.

4. Control Structures & Errors

  • Prefer Positive Checks: Use positive conditions to make code more intuitive.

  • Avoid Deep Nesting:

    • Use Guard Statements: Handle special cases early to reduce nesting.
    • Consider Polymorphism and Factory Functions: Simplify control structures by leveraging polymorphism and factory patterns.
    • Extract Control Structures into Separate Functions: Refactor complex logic into its own function.
  • Consider Using “Real” Errors: Prefer using exceptions and proper error-handling mechanisms instead of synthetic errors built with if statements.

5. Objects & Classes

  • Focus on Building “Real Objects” or Data Containers/Structures: Design classes to represent actual entities or structures that encapsulate data and behavior.

  • Build Small Classes with Single Responsibility: Ensure each class has one clear responsibility, though it may have multiple methods.

  • Build Classes with High Cohesion: The methods and properties of a class should be closely related and work together towards a common goal.

  • Follow the “Law of Demeter”: Avoid deep object chains like this.customer.lastPurchase.date to reduce dependencies and coupling.

  • Follow SOLID Principles: Especially in object-oriented programming:

    • Single Responsibility Principle (SRP): Each class should have only one reason to change.
    • Open/Closed Principle (OCP): Classes should be open for extension but closed for modification.
    • These principles greatly contribute to writing clean, maintainable, and scalable code.

By adhering to this checklist, you can write code that is not only functional but also clean, readable, and easy to maintain. Clean code helps in reducing bugs, improving collaboration, and ensuring that your codebase can scale and adapt over time.


Share


Related Posts

Trunk Based Development: Cara Kekinian Buat Ngembangin Software
August 27, 2024
1 min
© 2024, All Rights Reserved.
Powered By

Quick Links

Advertise with usAbout UsContact Us

Social Media