Effective Java 3/E summary chanpter 7 (Shorter version)
21 Mar 2021Chanpter 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
Interfaceis better thanClassEnumis better thanboolean- Avoid overloading when same size of paramters
- When using
varargs, define required params separatelystatic int min(int firstArg, int... remainingArgs) { ... }
Return
- Do not return
null - Return
Collections.empty*()replacingnull - Return
Optional<T>when- avoiding return
null - clients need to deal with return value specially
- avoiding return
Comments for all exposed API
- Write WHAT, not HOW
- Write @params, @return, @throws, postcondition, side effect, thread safety level