Skip to content

Recursion

August 12, 2009

During college recursion was always on a pedestal where only good programmers used and understood it. I got the basic idea, but never fully understood how to use recursion effectively. This always dug away at my confidence as a programmer.

Since, working in the field and becoming more aware of literature in the field I have come to realize that the use of recursion does not make you a good programmer.

Recursion tends to add complexity when a simple for loop would do the job. There are of course times that recursion might be the best solution, however, one must consider the trade-offs between readability/complexity and efficiency.

I have never used recursion in practice. The only times I have used recursion was when learning what and how it works in college. I would like to find in instance where it makes since to use, but higher level languages tend to restrict your options to enhance readability and structure.

I for one prefer readability over elegance.

Things to consider when using recursion from Code Complete (page 410):

  1. Does the recursive routine include code to stop the recursion?
  2. Does it use a safety counter or guarantee the routine stops?
  3. Is recursion limited to one routine?
  4. Does the depth of the recursion stay in-bounds of the program’s memory?
  5. Is recursion the best approach? Is it better then simple iteration?

Please remember that, it doesn’t matter what your code looks like if it is not being used.

Advertisements
One Comment
  1. Well put. Recursion tends to add complexity where there is none.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: