22

!rant example of effective cross-language communication. A Drake meme can save up to 140 characters on avg.

Comments
  • 1
    Ikr! Exactly my point yet some people managed to present me problems that are better done with recursion...
  • 4
    ++ for memes in communication, -- for saying that recursion is not useful at all! Try to search in a tree without recursion. You will have to use a stack and things will get messy.
    The algorithm to solve the Hanoi puzzle takes about 3 lines of code!

    # a, b, c are the stacks of rings
    # a is the source, c the destination
    # and b is used as a helper
    def hanoi(a, b, c):
    if not empty(a):
    hanoi(a[1:], c, b)
    move(top(a), c)
    hanoi(b, a, c)

    Now try to solve it with for loops. I assure you that the solution will be so much more complex!

    And, like that, there are numerous problems that are easier solved with recursion.
  • 0
    Disclaimer: the pseudo code in the previous comment is not totally correct, but it gives you an idea of the algorithm
  • 1
    @Joserc87 good for academical purposes, in years programming for loops >> recursion (business logic). No client will give you extra points for solving something recursively
  • 1
    @dootdoot no, they won't give you extra points for using recursive algorithms. They will give you extra points for finishing earlier. They will give you extra points for cleaner code. They will give you extra points for having less bugs.

    Recursive algorithms are not something theorical, they are very real and implemented everywhere in systems in production. Some problems are much more approachable in a recursive way, and the only reason to mess with loops and stacks is that the developer hasn't learned the recursive mindset.

    What exactly do you have against recursion?
  • 1
    @Joserc87 recursion doesn't help readability at all. Yes it is brief, but brief != readability
  • 2
    @dootdoot I completely disagree. To me, recursion is very readable (when done right of course). You just have to know how to think in a recursive way and get used to it.

    Do you have an example?
  • 0
    @Joserc87 I agree with most of what you say, but regarding the "readability issue", I think @dootdoot has a point.
    I mean, for a non-programmer or a novice programmer procedural code is much easier to understand than recursive code, you said it yourself.

    Therefore given the same syntax and descriptive naming of variables, functions and operations - procedural code is less complex to understand and thus more readable.
  • 1
    @bioDan Yes, but that is only applicable to those who doesn't have an understanding of recursivity. Recursive code is usually more complex than most regular, procedural code. What I'm saying is that recursive code is simpler and more readable than THE EQUIVALENT procedural code.

    Take the algorithm to search in a tree as an example. The recursive code is simple and readable. The procedural EQUIVALENT code involves a stack where we store the nodes that we have been visiting plus a while (not a for) that loops while the stack is not empty, a nested loop, etc. Chances are that a non-programer or novice can't understand the procedural version either ;)
  • 1
    Besides, I think code like this is very readable (regardless of whether you think it's complex), because it almost reads like english:

    Search(node, x):
    if node.value:
    return node
    else
    for child in node:
    found = search(child, x)
    if found:
    return child
    else:
    return None

    It reads like: "if the node contains x then return that node, otherwise search in the children". To me, the recursive description is so much easier to understand and definitely much more readable than the procedural version (which I can't type here)
  • 1
    @Joserc87 I agree with what you are saying, and I also love and am fascinated with recursion both in code and in nature. Also ofcourse there are cases which recursiveness is the preferred way to go.

    But let me give you a personal real life example regarding recursion and readability.

    My last workplace has a large codebase, i had to tdecouple advanced search options from minified variables and make sense out of them while they kept polymorphing themselves in a bounch of recursive functions.

    I can't recall the procedures themselves, I just remember that after about 2 hours of trying to make sense of it, I removed the code and I spent another 4 hours rebuilding it myself.

    (But I also used recursion :) )
  • 0
    @bioDan yup, so true. Recursion can be missused and then it becomes a nightmare xD
Add Comment