Skip to content

Instantly share code, notes, and snippets.

@zero88
Last active July 23, 2018 14:22
Show Gist options
  • Save zero88/88b5a638fc9005576c903f4153b94d45 to your computer and use it in GitHub Desktop.
Save zero88/88b5a638fc9005576c903f4153b94d45 to your computer and use it in GitHub Desktop.
Clean Code overview

Clean Code

Code for human not machines

Code is clean if anyone in team can understand, easy to read and enhance by other developer instead of origin author.

Code clean is readability, flexibility, extensibility, maintainability.

Bullet points:

  1. Keep code simple stupid.
    • Reduce complexity algorithm as muc as possible.
    • Return code early to keep code_indent <= 3
    • Avoid negative condition
  2. Follow one consistence coding standard for team.
  3. Naming convention
    • Variable/function/class/module name is meaningful, explanatory.
    • Name can be long if it is public, short name is acceptable if it is private.
    • Function/ Method name is Verb, meanwhile variable/class/module is Noun.
    • Extract magic number to constants.
  4. Don't repeat yourself: No duplicate code.
  5. Single responsibility
    • Single module/package contains a set of similar classes, that represents one campaign concept.
    • Single class wraps a set of similar functions, that represents one duty of campaign.
    • Single function covers one and only one purpose, and do it well.
  6. No god class, long method, many arguments/parameters in method
    • Class: line_of_code < 1000
    • Method: line_of_code < 50
    • Method parameters: total_parameter < 5
  7. Catching and logging exception even if exception is thrown in silent.
  8. Loose coupling: each software layer (presentation layer/business layer/persistence layer/database layer, etc.) or each part in MVC communicates to each other via abstract interface instead of concreate.
  9. Code modular: seprate functionality into independent and interchangeble module. Use more composite/component pattern instead of inheritance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment