Skip to content

Instantly share code, notes, and snippets.

@leeweiminsg
Last active June 22, 2020 12:11
Show Gist options
  • Save leeweiminsg/b31495b05136a29ceff86f5c4967a697 to your computer and use it in GitHub Desktop.
Save leeweiminsg/b31495b05136a29ceff86f5c4967a697 to your computer and use it in GitHub Desktop.
Clean Code Cheatsheet

Clean Code Cheatsheet

Clean code:

  • High ratio of reading to writing code
  • Leave code cleaner then when you found it

Names:

  • Reveal intention
  • Avoid disinformation
  • Make meaningful distinctions
  • Use pronounceable names
  • Use searchable names
  • Avoid encodings and mental mappings
  • One work per concept
  • Appropriate context

Functions:

  • Single responsibility
  • Single abstraction level
  • Step down rule
  • Switch statements
  • Descriptive names
  • Minimise arguments
  • No flag arguments
  • Dyadic functions:
    • Natural ordering of arguments
  • Monadic -> Dyadic -> Triadic functions:
    • Argument objects and lists
  • Verb and keyword naming
  • No side effects
  • Command query separation
  • Exceptions instead of error codes
  • Extract try-catch blocks

Objects and Data Structures:

  • Abstract data
  • Data object anti-symmetry
  • Law of demeter
  • Data transfer object

Classes:

  • Encapsulated
  • Small - single responsibility
  • Cohesive
  • Organise for change (subclass, interface)

Boundaries:

  • Wrap 3rd party interfaces
  • Use tests to learn

Error-handling:

  • Exceptions over return codes
  • Use try-catch-finally
  • Use unchecked exceptions (Java)
  • Have informative error messages
  • Define exception classes according to caller's needs
  • Special case pattern
  • Don't return or pass null

Testing:

  • Test-driven development (TDD) can lead to a lot of test code
  • Test code:
    • is as important as production code (PC)
    • keeps PC maintainable
    • changes with PC
    • readability is more important than in PC
    • does not have to be very efficient
    • good to have domain-specific testing APIs
  • One concept/assert per test
  • FIRST:
    • Fast
    • Independent
    • Repeatable
    • Self-validating
    • Timely

Emergence

  • Design rules:
    • Runs all tests
    • Refactoring
    • No duplication
    • Expressive
    • Minimal classes and methods

Code Smells:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment