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?