Monday, November 8, 2010

There and Back Again - A Developer's Recursion Tale

Recursive functions are so 2009. All the cool kids are now scrambling to convert those legacy recursive functions into stack based iteration. Why would you ever need this, you ask? Maybe you’ve heard of my friend “StackOverflowError”, he likes to hang out on the JVM. Or maybe you’re a browser kinda guy and you’ve seen your alert messages say “Stack Overflow”. Or worst case (like me), you’re supporting IE6 and you finally figured out that the “Browser shows a blank screen” defect you can’t reproduce is IE’s way of overflowing (oh yes it really is). Well, stack based iteration is a way to leave your nice recursive function the way it is, yet still avoid the overflow errors. You just have to keep track of the function stack frames yourself by adding a little boilerplate around your method (and even that can be cleaned up fairly easily).

So here is the tale of taking the road now commonly traveled. Learning to program imperatively, writing some beautiful recursive functions, and then ripping it out again because of platform limitations. There and Back Again – A Developer’s Tale of Recursion...


Read the full article over on the Canoo blog. And if you want to be kind and vote, then head over to DZone first.

3 comments:

Brian said...

Now you understand why some of us get annoyed at the lack of tail call optimization support in some environments (JVM, I'm looking at you!).

tania said...

JVM, never was that fun before for me until I was in this field.

- Tanya
Web Design Firm

Linda said...

JVM is also a good tool to get the solution of the most of the business problmes