Clean Code 3: Functions by Robert C. Martin (Uncle Bob)

This post is my personal notes of the talk “Clean Code 3: Functions” by Robert C. Martin.

Recently I revisited this conference talk Uncle Bob gave about functions/methods, where he gives us a set of guidelines on how to do them clean. These are the main guidelines I gathered:

  • Name your classes using a noun, according to their domain and architectural meaning
  • Name functions/methods using a verb, according to their context and what they do
  • Should be as small as possible
  • Should have an explicit intent, both in its name and code
  • Should do one thing only and do it well. One thing means one level of abstraction (manipulating strings is one level, manipulating business rules is another level), or when nothing else can be extracted and meaningfully reused in another function or method
  • Methods in a class should be organized per level of abstraction, where the methods used by a method sit directly below it
  • Choose descriptive names, for small functions, that do one thing
  • Should have at most 3 arguments, if we have more than 3 and they are conceptually related, we should group them in an object
  • Do not use output-arguments (arguments to output data out of a function/method)
  • Don’t use boolean arguments, use 2 functions/methods instead
  • Comply to CQS, either:
    – Do something (change state)
    – Get something (return info about an object)
    Not both
  • Don’t return an error code, throw an exception instead
  • Blocks inside conditionals or loops should be one line long, a function call which, if named correctly, adds documentary value

The video

Clean Code III: Functions – 2008 – Robert C. Martin

3 thoughts on “Clean Code 3: Functions by Robert C. Martin (Uncle Bob)

Leave a comment