Writing Effective Methods in Java

                Parag Shah

      http://www.diycomputerscience.com
Always check method parameters for validity
WHY ?

Because we should always fail fast !
An example
What will happen if we do not ?

We may get an Exception which is unrelated
We may get an incorrect result
We may create an object which will fail at a totally
 different and unrelated time
What should be checked ?

Constructors
Public, protected, package methods
When not to check ?

If the check is very expensive
  a method which expects a sorted list
If the check will be done implicitly in the
   computation
  sorting automatically checks for comparable
    instances
Possible to omit checks in private methods

Check methods params_for_validity

  • 1.
    Writing Effective Methodsin Java Parag Shah http://www.diycomputerscience.com
  • 2.
    Always check methodparameters for validity
  • 3.
    WHY ? Because weshould always fail fast !
  • 4.
  • 5.
    What will happenif we do not ? We may get an Exception which is unrelated We may get an incorrect result We may create an object which will fail at a totally different and unrelated time
  • 6.
    What should bechecked ? Constructors Public, protected, package methods
  • 7.
    When not tocheck ? If the check is very expensive a method which expects a sorted list If the check will be done implicitly in the computation sorting automatically checks for comparable instances Possible to omit checks in private methods