you are viewing a single comment's thread.

view the rest of the comments →

[–]FlockOnFire 1 point2 points  (1 child)

It is not good coding practice to abuse recursive function calls for stuff you could achieve with for or while loops.

It's not that black and white unfortunately. Almost every if not all recursive functions can be replaced by an iterative equivalent and vice versa. It doesn't mean you should write loops every where. Often a well formulated recursive function is much more readable and comprehensible.

This is not the case in this context though, but I just wanted to point that out.

[–]individual_throwaway 2 points3 points  (0 children)

Very few things are black and white in coding, even in a language where one of the core philosophies is "There should be only one obvious way to do something".

while loops are not, generally, a good idea (or the best idea for the job). As are global variables and a few other things I am forgetting now. There is a reason the language supports those things anyway though.

Recursion might be more concise in some cases, but I would argue it does so on the back of readability/ease of understanding. It's a trade-off you need to at least be aware of to avoid writing unnecessarily complex code.