Why should iteration be used instead of tail recursion?

后端 未结 2 1919
夕颜
夕颜 2021-01-17 08:24

What is the design smell, poor practice in recursion ? once I saw resharper suggesting improvements I quickly looked around on google. Saw numerous comments around re-factor

相关标签:
2条回答
  • 2021-01-17 08:54

    Because people mistakenly care more about micro-optimizations than clear and readable code.

    0 讨论(0)
  • 2021-01-17 09:02

    The recursion makes additional (recursive) function call, which also means a stack allocation, for every level (every object you handle).

    Generally the iteration is better because it doesn't make that additional call/allocation.

    Of course, the difference would be visible in case there are many objects to handle, which I suppose is not the case in your example. So for you it's not an issue and I guess the intention is to teach you a better practice in general.

    0 讨论(0)
提交回复
热议问题