Why should iteration be used instead of tail recursion?

后端 未结 2 1946
夕颜
夕颜 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 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.

提交回复
热议问题