Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Avoiding Recursion

 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just learned that posts in the book giveaway thread are ineligible, so I'm making a post here.

Although it's not always possible, I always try to avoid recursion because iteration is almost always faster.
In looping constructs, I sometimes avoid calculating a length each time:

Java performance techniques work with javascript and have a more pronounced effect because javascript is much slower.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Smith:
Although it's not always possible, I always try to avoid recursion because iteration is almost always faster.

And almost always, it probably doesn't matter - seriously.
I would *always* go with the solution that is easier to read (more intuitive) first and only think about reworking a recursion into iteration if profiling showed that it was a problem. Remember, the biggest cost factor in our industry is maintenance!

In looping constructs, I sometimes avoid calculating a length each time:


In my opinion using an Iterator to encapsulate the looping logic might be more appropriate.
 
Jack Shirazi
Author
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loads of the tuning techniques I cover are applicable to many languages. Over the years I have picked up techniques from other languages too. Lot's of the low level ones have inevitably been tried in C. Many of the OO ones were used in Smalltalk. Just as you don't restrict yourself to looking at only javascript specifc tips, I don't restrict myself to looking at Java performance techniques either. And I'm sure we're both the richer for that.
--Jack Shirazi
JavaPerformanceTuning.com
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I think it's how you design a program that can make a huge difference. Everything from the general program concept and approach to the algorithms, to the really language-specific stuff.

I would *always* go with the solution that is easier to read (more intuitive)

Sometimes recursive solutions are easier to spot, and if I think about it, I can sometimes find an iterative solution that is just as concise.
When I do this, I always choose the iterative solution.
Sometimes I don't see it right away. The more I do this, the easier it becomes.
I think Ilja's advice is good advice because it tends to lead to self-documenting code.
In some conditions, the differences between iteration and recursion may be noticeable. This is more often true (and to a greater degree) with javascript.
As for looking into other languages, I actually do program in Java, just not professionally. I'd like to get a job. Hey, then I could even afford to buy Jack's book!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic