DEV Community

Discussion on: Given a choice, would you choose an iterative approach or a recursive one?

Collapse
 
anwar_nairi profile image
Anwar • Edited

I do not use recursive often, like really not that much.

The only time I used it is when I found myself creating this old NodeJS package to "inline" codes imported in a file (using ES6 import syntax" down to the main file).

In this case, I needed to get an AST of the file, and, whenever I encountered an import "foo", I would get the AST of this file, and in this sub file, whenever I encountered an import "bar", I would get the AST of this file, and...

You got the idea 😂

So I guess recursivity made sense in this case because this was natural to think of a fractal way to solve this.

Else, I would do some sequential algorithm to solve my problems, which would eventually requires iterating.

Collapse
 
myterminal profile image
Mohammed Ismail Ansari

I'd definitely agree with you on that: it all depends upon the requirement for which one's more appropriate for that specific use-case. For me, in most cases, I get the hint within seconds of looking at the problem.