- #1
Jamin2112
- 986
- 12
I know someone at Google who says you should never have more than 1 return statement in a function. That seems ridiculous to me.
Let's take a simple example. Suppose we need a function that finds the maximum value of any C++ array.
I can do
or, equivalently,
Apparently, the first is considered bad because it has two return statements? The second seems very n00b-esque to me. (I know there's an even better way to do the whole function.)
Let's take a simple example. Suppose we need a function that finds the maximum value of any C++ array.
I can do
Code:
template <class T>
bool max (T* arrPtr, size_t n, T highest)
{
if (n == 0)
{
return false;
}
else
{
for (T* i(arrPtr), j(arrPtr + n); i != j; ++i)
if (*i > highest)
highest = *i;
return true;
}
}
or, equivalently,
Code:
template <class T>
bool max (T* arrPtr, size_t n, T highest)
{
bool retval;
if (n == 0)
{
retval = true;
}
else
{
for (T* i(arrPtr), j(arrPtr + n); i != j; ++i)
if (*i > highest)
highest = *i;
retval = false;
}
return retval;
}
Apparently, the first is considered bad because it has two return statements? The second seems very n00b-esque to me. (I know there's an even better way to do the whole function.)