Clean Code - Functions.

Notes from Clean Code - Chapter 3!

All points below are about functions..
  • Keep them small.
  • One level of abstraction per function.
  • Ideal number of arguments = 0, then comes 1.
  • Flag arguments are ugly, such as: void foo(Bar bar, boolean uglyFlag);
  • Argument Objects.. When a function seems to need more than two arguments, wrap arguments into a Class!
  • No side effects! Never! One function - one thing..
  • Command - Query separation: Either do something, or answer something!
  • Prefer Exceptions, do not return error values.
Also quoting from the book..
Some programmers follow Edsger Dijkstra 's rules of structured programming. Dijkstra said that every function, and every block within a function, should have one entry and one exit. Following these rules means that there should only be one return statement in a function, no break or continue statements in a loop..
I do not think one exit means one return statement in a function, rather it means a function must have only one return point. So it does not matter how many return statements you have. Here is an example..
void bar() {
    foo();
    // Foo will return here, 
    // no matter which return statement it returns from.
    // Single return point, not single return statement!
}
 
void foo() {
    if(!isRequired) {
        return;
    }
 
    doSomeThing();
 
    if(notRequiredAnymore) {
        return;
    }
 
    doOtherThing();
    return;
}