[閱讀筆記] Clean Code — Chapter1/Chapter2

Chapter1: Clean Code

  • What is clean code and bad code?
  • The importance of writing clean code

Chapter2: Meaningful Names

  • Use Intention revealing names
    int d; // elapsed time in days
    int elapsedTimeInDays;
  • Avoid Disinformation
    accountList which isn’t a list
  • Make Meaningful Distinctions
    ProductInfo and Product
  • Use Pronounceable Names
  • Use Searchable Names
    the length of a name → the size of its scope
  • Avoid Encodings
    Hungarian notation (type naming)
    Member Prefixes (m_dsc)
    Interfaces and Implementations (IShapeFactory)
  • Avoid Mental Mapping
    single-letter variable names
  • Avoid Noise Words
    Manager, Processor, Data, Info
  • Class Names
    noun rather than a verb
  • Method Names
    verb or verb phrase (get/set/is)
    using static factory methods when overloading the constructor
    Complex fulcrumPoint = Complex.FromRealNumber(23.0);
    Complex fulcrumPoint = new Complex(23.0);
  • Be wary of hard to name
    the offending code is doing too much/too less/don’t understand the domain well enough
  • Don’t Be Cute
    say what you mean, mean what you say whack()vs kill()
  • Pick One Word per Concept
    fetch/retrieve/get, controller/manager
  • Don’t Pun
    avoid using the same word for two purposes (add/insert)
  • Use Solution Domain Names
    choose technical names (AccountVisitor vs JobQueue)
  • Use Problem Domain Names
    separating solution (programmer) and problem (designer) domain concepts
  • Add Meaningful Context
    place names in context by enclosing them in well-named classes, functions, or namespaces
    firstName/stateaddrFirstName/addrStateAddress
  • Don’t Add Gratuitous Context
    shorter > longer if it is clear enough
    avoid prefixed: distinct the MAC address and web address?
    Create MAC, URL class (instead of MACAddress)

An interactive engineer based in Tokyo. CG/Unity/Coding/Book Review