views:

72

answers:

1

Should I validate input parameters on all functions I create? Input isn't passed from a user, but from other routines as part of an automatic process.

I use error handling. I'm not sure what more validating input could do for me. If the input isn't valid, that's pretty much the same thing as an error, isn't it?

+1  A: 

A lot will depend on how public your function is and how destructive bad input will be to your application/data-store.

If there is a tight coupling between the code calling the function and the function itself I think validation is overkill and will only add complexity to your solution.

However, if your code is, for example, part of a library used by more than just you/your code add as much validation as needed. You don't want it to be your code that fails/allows something wrong to happen because you didn't validate the input.

Think of libraries/functions you already use. If you chuck bad values at them, how do they respond? Do they respond with a tailored error (e.g "Elephant" is not a fruit) or an internal error which is a by-product of your bad input (e.g "Object reference not set to an instance of an object")?

CResults
Thanks. Forgive me for this stupidity, but what does "tight coupling" mean? I see that a lot.
EdOxH
In this instance I mean that the two functions work *very* closely together for the same common goal and changing one would have a negative effect on the other. Have a look at http://en.wikipedia.org/wiki/Coupling_(computer_science)
CResults