Q:

Should I use an else when an if contains a return?

I am following some examples in the book, and I noticed two different conventions for having various return conditions. Is there any difference between the two?
//example 1
if(someCondition)
{
   return (someValue);
}
return (someOtherValue);

//example 2
if(someCondition)
{
   return (someValue);
}
else
{
   return (someOtherValue);
}

Personally, I like the second example better, because it's more explicit, and I feel that it's more readable.

A:

If your language contains a conditional operator I would recommend using that.
return condition ? ifTrue : ifFalse;

A:

There is no difference other than the look.  Both will return the same, regardless of which you choose.

A:

Personally the case where this comes up a lot is when you are eliminating invalid states from your function/method call.  For example:
sqrt(x) {
    if(x < 0)
        return 0;

    answer=math;
        return(answer);

If you use elses, you can end up with a lot of really horrific nesting.
This also involves the "Single return" theory--lots of people think a method should only have one return statement.  This also leads to mess in some cases.
In your specific example it's 50/50 IMO.
Do what you like, but I highly suggest giving the "Test immediately and return for simple cases" theory a try.

