Skip to main content
Post Closed as "Duplicate" by CommunityBot
edited tags
Link
yannis
  • 39.7k
  • 40
  • 185
  • 218
Fix tags
Link
user8
user8

Should a method validate its parameters?

Tweeted twitter.com/#!/StackProgrammer/status/54787535062302721
Source Link
Amit Wadhwa
  • 2k
  • 1
  • 15
  • 20

Should a method validate its parameters

Say you are designing a Square root method sqrt. Do you prefer to validate the parameter passed is not a negative number or do you leave it up to the caller to make sure the param passed is valid. How does your answer vary if the method/API is for 3rd party consumption or if it is only going to be used for the particular application you are working on

I have been of the opinion that a method should validate its parameter, however, Pragmatic Programmer in its Design by Contract section (chapter 4) says it's the caller responsibility to pass good data (pg 111 and 115) and suggests using Assertions in the method to verify the same. I want to know what others feel about this.