Skip to main content

Notes from the Clean Code book - 3

Chapter 2 (Meaningful names)

Create correct names

  • The length of a name MUST match it’s scope size
  • The name of a class MUST NOT be a verb
  • Methods MUST have verb names
  • Don’t be afraid of changes regarding variable names (as long as it’s for the better)

Reveal the intention

  • Why does it exists?
  • What does it do?
  • How it’s used?
  • If a name requires a comment, then it’s not revealing it’s intention

Avoid disinformation

  • DO NOT user names which are away from it’s intended usage meaning
  • Avoid names with minimal variation:
    MethodUsedForHandlingOfStrings
    MethodUsedForEfficientStorageOfStrings
    

Meaningful distinction

  • DO NOT create code targeting only the compiler/interpreter
  • Names with numerical series (a1, a2) do not hold an intention, they misinform
  • Additional words are redundant (e.g: a, an, the, variable, info, data, table, NameString)
    // different names but same meaning
    class Product
    class ProductInfo
    class ProductData
    

Pronounceable

  • Use pronounceable names (humans can do words fine, if it can’t be pronounced it can’t be explained)

Searchable

  • Single letter names and numerical constants are NOT easy to find

Avoid encoding

  • Encoding types or scopes into a name it makes harder to decode
  • It IS NOT reasonable for new employees to learn yet another language of the encoding on top of the code they will work on.

Avoid mental mappings

  • Single letter variables are OK for loop counters
  • Avoid the reader having to map the variable with a concept mentally (explicit is better)
  • The difference between a smart programmer and a professional programmer is that the later knows that clarity is what matters

One word per concept

  • Avoid synonyms for equivalent methods (get, fetch, retrieve)
  • A coherent lexical is an advantage for programmers using your code

Avoid word confusions

  • DO NOT use the same word if there’s semantic difference:

    A method that adds values to an existing object is different to a list of the same object, which has a method to add new elements. Using the same word creates confusion for which there is a semantic difference