Validate input parameters in VB?
Should I validate input pa开发者_StackOverflowrameters 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?
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")?
精彩评论