knottythots label

18

Iterative vs Recursive

Filed under: School, Programming

There are quite a few things taught in this semester that we have to know because its in the exams but not recommended to use in real life.

This semester, the programming language taught was C++. Things like continue, break and others were discourage from use unless necessary. The major portion that has very heavy weightage for both modules this semester is recursion. At the same time, the lecturers are telling us how this is discouraged in the real world. Ironic. I understand we have to learn this but why the heavy weightage if its discouraged. It’s like teaching <font> tags to beginners then telling them they should never have to use it.

Using iteration over recursion is usually an obvious choice. Not only is it easier to understand, it takes lesser time and is more efficient as compared to recursion.

Of course, recursion is not without it’s merits. The lecturers like to put them as "simple and elegant". In a more practical sense, there are times where things can’t be accomplished using iteration. I’ve also read that recursion reduces the length of code but I feel that is pretty subjective. Depending on the algorithm, codes rewritten for recursion may end up being more complex and longer. One of the major disadvantage is of course the problem of causing a stack overflow. The solution to this is tail-recursion calls which is a variant of the versions that we are taught (non-tail). Tail-recursion calls do not require additional stack space.

Also, I came across an article from Microsoft containing a short segment on comparing the two. Most I found were in favour of iteration. This is one of the few that have stronger supportive points for recursion over iteration.

Object-orientated language or no, I’m still sticking with iteration despite having it’s performance problems fixed simply because the code will be simpler to understand. Besides, things are going towards the way of object-orientated programming in which recursion has a slight disadvantage.

Darn the heavy weightage on recursion. It’s good to learn and know how to at least. Every programmer has his or her own style but this is almost like doing hard-core training for us to be functional programmers.

I wish I could say "To each his own" but the university isn’t letting me. Sigh.

Comments »

The URI to TrackBack this entry is: http://knottythots.blogsome.com/2007/11/18/iterative-vs-recursive/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.