This SLOG I’m going to revisit my thoughts on
recursion. This SLOG might be a bit shorter because I might be rehashing some
old ideas.
My previous thoughts on recursion were quite harsh. My
main issues with it were that it's
difficult to read, it's hard to create a mental image on how it works, and it
is proving difficult to debug. I still agree with my main sentiment, recursion
should be avoided whenever possible. However I have toned down my stance on the
topic.
I originally found
recursion difficult to read. I still 100% agree with this. Despite becoming
incredibly familiar with recursive code I still find it a huge pain to read. I
am definitely not alone with these thoughts, I have talked to classmates, upper
years, as well as professional software developers. I found it incredibly
interesting when one of the software developers were told me that recursion is
usually frowned upon during most projects. When I asked why, he said that when
new people are brought onto a team, recursive code can be very difficult for
them to understand how the pre-existing code works.
My next point was that recursive code is difficult to
visualize. A work around for this is drawing diagrams. While it is hard to
visualize, even the most artistically challenged (AKA me) can draw a crude
diagram that represents how an algorithm works. So I find this to be
significantly less of an issue than I did previously
My last point was that recursive code is
difficult to debug. My experience with minimax is first hand evidence with
this. The most frustrated I have been all year was having to debug the minimax
algorithm. The main difficulty with going through the code was that it hard to
see exactly how the code was looping. With iteration it’s very easy to see how the
program loops. With iteration it is usually much easier to see where the code is
going to end. With recursion it is exceedingly difficult to see what the code
is going to return.
Although I do have some apprehensive thoughts about recursion,
I can see how it is very useful. As I’ve been working with recursive code more
and more throughout the semester I have gotten more comfortable working with
it. Recursion in some instances how for easier manipulation of abstract data
structures such as binary trees, linked lists, and many others. So while I find
it challenging to work with I do accept that it is needed.