Check methods params_for_validity

526 views

Published on

This presentation explains how and why we should check method parameters for validity as a good programming practice in Java.

Published in: Technology, Economy & Finance
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
526
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Check methods params_for_validity

  1. 1. Writing Effective Methods in Java Parag Shah http://www.diycomputerscience.com
  2. 2. Always check method parameters for validity
  3. 3. WHY ?Because we should always fail fast !
  4. 4. An example
  5. 5. What will happen if we do not ?We may get an Exception which is unrelatedWe may get an incorrect resultWe may create an object which will fail at a totally different and unrelated time
  6. 6. What should be checked ?ConstructorsPublic, protected, package methods
  7. 7. When not to check ?If the check is very expensive a method which expects a sorted listIf the check will be done implicitly in the computation sorting automatically checks for comparable instancesPossible to omit checks in private methods

×