Style of if: to nest or not to nest

Posted by Marco on Stack Overflow See other posts from Stack Overflow or by Marco
Published on 2010-05-26T10:13:38Z Indexed on 2010/05/26 10:21 UTC
Read the original article Hit count: 245

A colleague of mine and me had a discussion about the following best-practice issue.
Most functions/methods start with some parameter checking.

I advocate the following style, which avoids nesting.

if (parameter one is ugly) return ERROR;
if (parameter two is nonsense || it is raining) return ERROR;
// do the useful stuff
return result;

He, who comes from a more functional/logic programming background, prefers the following, because it reduces the number of exit points from the function.

if (parameter one is ok) {
   if (parameter two is ok && the sun is shining) {
      // do the useful stuff
      return result
   }
}
return ERROR;

Which one would you prefer and why?

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about subjective