Following on from the previous rule, the usual way a method functions is this:

  1. The method gets passed some input parameters which are validated.

  2. It does some work using those input parameters, so it is important they are valid.

  3. A return value or exception can be generated as the result of the work.

If the method is public, then the input parameters can be provided by any other programmer's code, of all skill levels, so parameters should be treated with distrust until validated.

Java 1.5 users can use assertions to validate method parameters, remembering that these are disabled unless a special JVM flag is set, so they provide little protection from production runtime problems.  It is far better to write your method so it is robust and does not expect to run in a world where all developers are geniuses.  

blog comments powered by Disqus