views:

110

answers:

3

Greetings,

I'm working in a code base that uses alot of anonymous methods, where the anonymous methods are chaining other anonymus methods that call the same thing the first one calls.

main()
{
 anonymous1(); 
}
anonymous1()
{
  // call anonymous2 
}
anonymous2()
{
 //call anonymous3
}
anonymous3()
{
  // Call anonymous1
}

thats the basic break down, sorry for the over simplification.

My concern is that one of the anonymous methods are causing problems chaining the calls like that. IMO it looks like it's just bad recursion that is going to cause a stackoverflow exception.

Thanks for your help in advance.

+2  A: 

It'll all depend on how you implement logic.

Recursion by itself is not and should not be a problem.

One could argue the same about any file/folder algorithm that uses recursion. If it is well implemented, you have nothing to worry about.

devoured elysium
A: 

Unless the compiler or the JIT optimizer applies tail call optimization (which it probably won't, unless you're running on the .NET CLR version 4 in non-debug mode and your specific code allows that kind of optimization), recursion obviously bears the potential risk of a stack overflow.

The question here is, does that code recurse? And if so, does it stop soon enough, or will it recurse indefinitely? You should only expect real problems in the last of these cases. Thus I agree with devoured elysium's answer that it eventually depends on the logic.

Probably the code you have there could be structured better -- but that's impossible to judge without something more concrete to look at, IMHO.

stakx
A: 

Its not really the anonymous methods that are the problem, even though them being anonymous can make things a tad bit harder to keep track of. The code in the methods themselves is what would cause a stackoverflow or any other types of problems. As long as the code is as clear as possible and accomplishes its task well, you don't really need to worry about the structure.

There's nothing wrong with recursion, and you can allay any of your doubts with some testing and contemplation of your needs.

CrazyJugglerDrummer