Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What is in your opinion the best way to handle (at runtime) the situation when a more than one input parameter to a function is incorrect.

I can see two ways.

First one - a simply return an first error (by raising the exception or via some other way supported by a programming language) telling that the first of incorrect parameters is wrong.

Alternatively, the function can check all the parameters, combine all the error messages into one and return it as a one.

Or there might be some other approach.

What would be your preference and why?

Thank you.

share|improve this question

2 Answers

up vote 2 down vote accepted

There is no single correct answer to this.

Sometimes you want to fail fast - e.g., if detecting the errors is expensive and you know that with a single error you cannot proceed.

Other times you want to give complete information back to the user so they can correct all the issues at once.

I run into the latter situation more frequently than the former, but your experience may differ.

share|improve this answer
1  
+1 I prefer the approach that validates all inputs and returns a complete list of problems. Nothing is more annoying than testing/using a process that fails after several minutes and reports a single problem - only to fail again on the next attempt (after fixing the first problem) and report a different problem. If it's possible to report all problems at once, I'd say it's preferable. – FrustratedWithFormsDesigner May 2 at 15:00
@DanPichelman My preference is to give the complete information back (as in my case detecting the error is not expensive) but in a majority of the code I see it is not the case - and that is setting up the trend. So when I return the complete error information most of the consumers of my code (developers) are quite surprised (and I am aiming to get the opposite reaction) as they don't expect that. – Mitten May 3 at 13:57
I agree with you. 'Fail Fast' is a special case. If you're validating a large set of parameters and the first one you check is the primary key to all the rest, then there's not much point in processing further. Compilers sometimes kick out a "too many errors" error for similar reasons (although a compiler that returned one error at a time would drive me nuts). – Dan Pichelman May 3 at 14:06
1  
A good counter example is forms validation. When you fill in a form incorrectly, you definitely want all the errors at once. – Dan Pichelman May 3 at 14:06

Depnds on usage. Sometimes its simply right or wrong and if wrong there is nothing that can be done in reaction, so noone can benefit from knowing all ways in which it was wrong. Sometimes reaction is to ask for corrections. In such case its better to report.

Sometimes you don't know how it will be used.

So, if you are the user of such method, use the simplest thing that really work.

If its library, consider allowing user to specify error strategy.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.