Skip to main content

clean code by Uncle Bob

  • Code rot
  • wading code
  • different schools of thought, like Jiu Jitsu

Naming

  • implicit naming vs explicit naming explicit naming when you have those long names, no need to guess. Implicit naming requires mental mapping. C means count.
  • Don't show off your IQ with implicit names, one liners, etc... As a code author, you write for the other devs reading your code.
  • Meaningful distinctions Make differences between named things obvious. Bad example
getActiveAccount();
getActiveAccounts();
getActiveAccountInfo();
  • Pronouncable names

  • Searchable names

  • Avoid encoding if you can

    • Type encoding (Hungarian notation). Uncle Bob doesn't like I interface prefix.
    • Scope encoding. m_ for class members, in Java. global tmp etc
  • Class/obj - noun, method - verb

  • No culture dependent jokes like eatMyShorts() instead of abort()

  • One word per concept If add means add to array, then for math addition, use another word like sum

  • Contextual grouping street, zip, state, country. When not grouped, you stare at const state and wonder wtf? but if grouped in
    mailingAddress.state then it's clear.
    address - in "postal context" it's clear. But in other contexts, is it MAC? URL? IP?

  • Renaming in old code is OK. If you can improve things, do it.