tags:

views:

142

answers:

5

Yes, I know the wording is hard to understand, but this is something that bugs me a lot. On a recent project, I have a function that recurses, and there are a number of conditions that would cause it to stop recursing (at the moment three). Which of the situations would be optional? (I.E. best performance or easiest maintenance).

1) Conditional return:

void myRecursingFunction (int i, int j){
    if (conditionThatWouldStopRecursing) return;
    if (anotherConditionThatWouldStopRecursing) return;
    if (thirdConditionThatWouldStopRecursing) return;

    doSomeCodeHere();
    myRecursingFunction(i + 1, j);
    myRecursingFunction(i, j + 1);
}

2) Wrap the whole thing in an if statement

void myRecursingFunction (int i, int j){
    if (
        !conditionThatWouldStopRecursing &&
        !anotherConditionThatWouldStopRecursing &&
        !thirdConditionThatWouldStopRecursing
    ){
        doSomeCodeHere();
        myRecursingFunction(i + 1, j);
        myRecursingFunction(i, j + 1);
    }
}

3) You're doing it wrong noob, no sane algorithm would ever use recursion.

+3  A: 

I would opt for the first solution, since this makes it perfectly clear what the conditions are to stop the recursion. It is more readable and more maintainable imho.

Frederik Gheysels
+1  A: 

I like the variant 1 better...

It is much easier to read then variant 2. Here you have to understand 3 negations and chain them all together with and. I know its not "hard", but it takes much longer then glancing on variant 1

Heiko Hatzfeld
+6  A: 

Both of those approaches should result in the same IL code behind the scenes since they are equivalent boolean expressions. Note that each termination condition will be evaluated in the order you write it (since the compiler can't tell which is most likely), so you will want to put the most common termination condition first.

Even though structured programming dictates the second approach is better, personally I prefer to code return conditions as a separate block at the top of the recursive method. I find that easier to read and follow (though I am not a fan of returns in random areas of the method body).

Eric J.
+3  A: 

If it's something that needs to be fast, I'd recommend hitting the most common case as quickly as possible (i.e. put the base case at the end because you'll only hit it once). Also think about putting a base case-1 before the recursing clause (i.e. perform the test before you call the function again rather than check it on entry to the subsequent call) if that will make a difference.

And, with all things, don't optimise unless it's a problem. I'd go for clarity first.

Joe
I doubt it makes a difference. You still have to test for the base case before recursing regardless of what order the returns are in.
recursive
If it's either-or, yes. I was thinking of functions that have more then one flow (e.g. switch / case statements or nested ifs). But for this question, yes, you're right.
Joe
A: 

My vote is for option #1 as well. It looks clearer to me.

Alex Baranosky