Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@Artemix Why "worst solution possible"? I find recursive code to be quite elegant.
Yeah, there is a danger of a stack overflow, but if you're sure of the inputs that will go into the method, it can be quite a powerful way to solve problems. -
@Artemix Good point. Anyway, it's still useful for solving a certain class of problems. As long as we know exactly where it works well, and keep its drawbacks in mind, it's just another tool we can employ.
-
@vinaysshenoy Indeed. When the only tool you have is a hammer, everything looks like a nail. ;)
-
Please do not implement recursion everywhere. Let me give you an exercise: try to write a recursolution for something where you need more than 10,000 loops/instances/whatever/you know what i mean. Your solution will never work nicely, because: maximum recursion depth. Loops handle that shit with like foo bar.
-
@darksideplease Yeah, I get that. My loop will run 2-3 rounds at max. As I said, I know exactly what's going into the function.
-
pily5208y@vinaysshenoy recursive funcions are also often risky, as can be used by a malicious user to break your system. Like, if you have a method to find the min between n numbers an that is recursive, at the lowest grade you have n method headers in your memory. Make n big and you can get a stack overflow.
-
To the people who said recursion is bad:
We have been far too polluted with the idea of loops and mutations with the Algol family of languages.
Elegant programming languages like Lisp don't even have any looping consutucts.
The Algol family became popular because the machines were not powerful enough and it was much closer to assembly (in terms of adaptation). We are not in that era anymore and the machines are powerful enough to handle languages like Haskell lisp.
I can confidently say that recursive solutions are the most intuitive way to solve problems relative to their looping counterparts. Lot of popular languages internally does optimizations to handle very large recrusive calls. This opinion is from personal realization and conversations with experienced programmers who have delved deep into both family of languages -
@Artemix I disagree.
Recursion forces the user to think about the smallest problem first.
It easily offers more code readability and can solve complex problems in minimal readable lines.
Coming to the performance, the optimizations easily improve the performance. Recursion does not always mean huge function call stacks, a lot of optimization basically evaluates it to be a simple expression. -
@frozentruth dude, what did you smoke? Recursion often makes the code LESS readable. Also it is almost always in practice not useful when iterating over a big list of something, because - maximum recursion depth. And just setting the recursion depth number higher is not a practicable, yet future proof solution.
-
@darksideplease I don't smoke xD
Let me use your own example. Consider a big ass list that you want to transform. Modern FP languages will beat the crap out of iterative solutions. Imagine the code you have to write to make it concurrent in the iterative approach.
Coming to the readability part, take this with a pinch of salt. The understanding of recursion is a journey. Unless people have been through that journey (for ex: try solving simple problems with scheme), the beauty of recursion is never realized. -
@frozentruth beautiful yes. And yeah if you code in scheme or lisp then i agree. But not for the commonly used languages, eg java, php, c[\+#]*, python, ...
-
@Artemix me too. Festival (what i'm currently testing) uses scheme as a scripting interface..
-
@Artemix @darksideplease lol Lisp was used as an example. The points that I am trying to make is really evident in lisp. just use the example of Clojure instead.
Also speaking about real world, WhatsApp was built using Erlang. Facebook spam filter uses Haskell.
Imagine the use of map reduce in the real world systems.
Related Rants
That feeling when recursion finally "clicks", and you begin instantly identifying candidates in your code for recursive solutions.
undefined
power
algorithms
recursion
feels