To Recurse or Not To Recurse
Posted by Jim at November 16th, 2004
I’m still screwing around with XML and Java and will likely be doing so for quite some time.
One thing I’m currently meditating upon is whether or not to use recursion in the function that allows me to find particular nodes in my XML document.
On the one hand, all the examples I’ve seen use recursion and I like the elegance of recursion. The simplicity of it appeals to my sense of aesthetics. On the other hand, I’m aware that recursion is often slower than a less appealing iterative design.
Aesthetics or speed? The code will be evaluated by CS profs who probably like recursion themselves. But… if I’m lucky enough to have it evaluated by actual users, the speed might matter–or it might not. These are web pages after all. Not novels. Chances are, the files will be small enough that using recursion won’t slow things down measurably.
I plan to come to a decision sometime soon.
Your coding is all modular anyway, right? So write it both ways. Then you can test and swap at will.
James
A good point. But then I’ll have to spend more time on one of the less interesting parts of this project. It may be worth doing though–just to know which works better.
Jim Zoetewey
You are considering optimizing this code before you even write it. What did Don Knuth say about premature optimization?
Bottlenecks almost never happen to exist in exactly the places where you happen to know a faster but less elegant way to write things. And optimizing where there is no bottleneck is a waste of time.
Ed Heil
Also a good point.
Jim Zoetewey