Effective Java summary By Doo and Cheolho

Effective Java 3/E summary chanpter 7 (Shorter version)

Chanpter 7 : Method

You can find the longer version of this summary here.

Parameters

  • Consider protective copied instance to prevent an attack
  • Validation early + Documentation
    • Null check
    • Range check
    • Type check
  • Minimize parameters
    • Highly orthogonal
    • Use static member class
    • Use static member class + Builder pattern
  • Interface is better than Class
  • Enum is better than boolean
  • Avoid overloading when same size of paramters
  • When using varargs, define required params separately
      static int min(int firstArg, int... remainingArgs) { ... }
    

Return

  • Do not return null
  • Return Collections.empty*() replacing null
  • Return Optional<T> when
    • avoiding return null
    • clients need to deal with return value specially

Comments for all exposed API

  • Write WHAT, not HOW
  • Write @params, @return, @throws, postcondition, side effect, thread safety level