Daniel Kargl
Daniel Kargl

Reputation: 21

Replacing while-loop with recursion

Quick and simple question:

Is this:

private static void SetupConnection()
{
    try
    {
        TcpClient client = new TcpClient(myServer, myPort);
        //Do whatever...
    }
    catch (SocketException)
    {
        //Server is closed. Retry in 10 minutes.
        Thread.Sleep(600000);
        SetupConnection();
    }

a viable alternative to this:

private static void SetupConnection()
{
    while (true)
    {
        try
        {
            TcpClient client = new TcpClient(myServer, myPort);
            //Do whatever...
            break;
        }
        catch (SocketException)
        {
            //Server is closed. Retry in 10 minutes.
            Thread.Sleep(600000);
        }
    }
}

While the second one looks "cleaner" I am still rather curious if the first one is also acceptable – and if it isn't, then why not?

Upvotes: 0

Views: 950

Answers (2)

Manoj Choudhari
Manoj Choudhari

Reputation: 5624

Why Recursion is Bad ?

You will need to understand what is call stack.

This is a stack which is maitained in the memory. Every time a new method is called, that method's reference and parmaeters are added in this stack. This stack will be maintained in memory (RAM).

With recursion, if you keep on calling method without any boundary condition, the stack will keep on growing.

After some time, it would be in a state where it cannot accept any further entries because there is not enough memory to hold it.

That's when you will get "Stack Overflow Exception".

Can we rewrite every recursive algorithm without using recursion ?

Yes you can. The aproach is generally called as "Iterative approach".

In every case, you can use an auxillary stack / list / group of variables - to hold the parameters you were using in recursion.

Then you can iterate over these variables, until you reach the boundary condition.

That way, you can achieve the same result without calling your method again and again. It is always better to use this approach.

Then why people write recursive algorithms ?

Sometimes, recursive algorithm is very easy to read. Iterative approach code may not be easy to read and that's why people try to write recursive code many times.

You can decide whether to use iterative approach or recursive approach based on two things:

  • Input samples

  • Code maintainability

If you still want to write recursive method, DO NOT forget to add the boundary condition after which recursion will stop.

For ex. Tree travesal code (pre order, in order , post order) is easier to understand if you write recursive algorithm. This will work fine as long as you have some limit on number of nodes / levels in the tree. If you already know that your tree is very huge, probably you would go for iterative aproach.

Hope this helps you to understand these approaches better.

Upvotes: 1

Nick
Nick

Reputation: 5042

Recursion is bad in this case, because if your program runs for too long and connection is retried, you will eventually hit a StackOverflowException.

Upvotes: 6

Related Questions