3 ms·
Recursion, though it gives me a big programmer woodie because of the beauty of the code, in my opinion is a poor programming practice. You can always use a loop
by CodeWriter23 11y ago
Recursion, though it gives me a big programmer woodie because of the beauty of the code, in my opinion is a poor programming practice. You can always use a loop in lieu of recursion.
- daniel-levin 11y agoUnless of course the language is designed and optimised for it. For instance, Jane Street use OCaml - in which recursion is a standard language primitive - and they sometimes prove their mission critical code correct.
- Fr0styMatt8 11y agoWhy do you say that recursion is a poor programming practice (curious)? What if the solution using recursion results in more readable or declarative code? (especially with languages/compilers that do tail-call optimization properly?)
- hliyan 11y agoIt's the most optimal if readability is the goal, but not if reliability is the goal. Programmers often assume ideal interpreters/compilers and runtime environments. This is often not the reality, especially as the environment gets closer to hardware. Recursive implementations in particular can more easily expose peculiarities in the stack implementation, yes?
- jowiar 11y agoReadability is a very important contributor to reliability -- minimizing WTFs is important. That said, writing code that fits the construction of the language is probably more useful than siding between "loops or recursion". If I'm writing Scala, my code is usually chock-full-of-recursion. If I'm writing C, not really as much, because I'm thinking "which bits go into which memory". With Python I tend to think in terms of list comprehensions, etc.
- Htsthbjig 11y agoRecursion is perfectly ok when the code is not mission critical, for example for rapid prototyping. Recursion is A BIG ERROR for mission critical software because it needs variable memory in the stack that depends on the implementation and depends on the other memory on the stack(depending on where it is called), creating stack overflows, breaking completely the system. This means that you write software today for a car, it works fine. Tomorrow you reuse it for a motorcycle and only on specific cases it breaks dramatically, because the motorcycle people used another debugger or another OS with different assumptions. Now when you debug something, you expect errors, and errors in the new code. But when something already runs and fails sporadically, it is extremely difficult to isolate the bug, because you can't see the bug but the cascade consequences of the bug. "What if the solution using recursion results in more readable or declarative code?" Recursion is something that looks perfectly ok in code,on paper could look gorgeous, but could fail terribly in the implementation. The judge is not interested, when you say to her: yeah, it broke and killed someone, but it looks ok in the code! "(especially with languages/compilers that do tail-call optimization properly?)" If compilers were perfect, it will be no problem. On real life no compiler is perfect. We design compilers and they are pretty stupid, it is really hard to represent all the complexity of the world for any given possibility in a compiler.
- jacquesm 11y agoSome languages require you to use recursion. For instance Erlang, without recursion you're not going to get very far.
- brohee 11y agoYeah and they have TCO, which C doesn't. It's not recursion that is bad, it's unchecked stack growth, in environments where beancounters made sure the hardware is adequate but not more...
- jacquesm 11y agoC does have tail call optimization in some instances, that's a function of the compiler and while not all of the compilers can do this some can: http://ridiculousfish.com/blog/posts/will-it-optimize.html http://ridiculousfish.com/blog/posts/will-it-optimize.html C the language does not specify how optimizing compilers should handle this situation. -O2 on gcc will get you this optimization (and many more), it will even do TCO on really hard cases, some where FP languages/compilers would fail!
- karmakaze 11y agoOff-by-one errors must certainly more common in loops than in writing the base case of a recursive function--because the recursive description is more clear. This is just about knowing the environment your code runs in. If the compiler doesn't optimize tail-calls and you'll have limited stack, then yes, it's the wrong choice.