Alex Rivera | Logout

Do you put the error condition first or last?

Asked 2008-11-24T21:22:03.710
16

Which is better in general? Do you put the fault condition at the top or bottom?

if (noProblems == true) {
    // do stuff
} else {
    // deal with problem
}

Or:

if (noProblems == false) {
    // deal with problem
} else {
    // do stuff
}
Edit
Report

1 Answer

0

It's a very individual sort of decision, but I tend to put my error conditions first, under the pretense of grouping like code together. That is, if I do something, and it fails, the check for that failure is really actually part of doing that something; if I put the check for failure and action based on that at the beginning of my code, I've grouped my action and the complex response to that action together (working on the assumption that a failure result is actually a more complex return case than a success).

answered 2008-11-24T21:29:01.043

Your Answer