递归通常是一种不好的做法吗? [英] Is recursion a Bad Practice in general?

查看:60
本文介绍了递归通常是一种不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为那些让你的代码难以遵循但可以避免的事情被认为是不好的做法.递归似乎是其中之一(更不用说它可能导致的问题,如堆栈溢出等),所以当我们不得不在编程课程中认真学习使用它们时,我有点惊讶(因为它有时会导致更短代码).在我看来,教授之间也存在分歧......

I always thought things that make your code uneasy to follow while being avoidable are considered as Bad Practice. Recursion seems to be one of these things (not to mention the problems it can cause like stack overflowing etc.), so I was a bit surprised when we had to learn working seriously with them during a programming course (for it ocassionally results in shorter code). And it seems to me there is a disagreement about it between the professors too...

人们已经针对语言(例如 Python,其中评论将其与 goto 语句进行了比较)提出了这一问题,主要针对某个问题,但我对一般性感兴趣:

People already asked this specific to languages (like Python, where a comment compared it to a goto statement), mostly specific to a problem, but I am interested in general:

在现代编程中,递归是否被认为是一种不好的做法?我什么时候应该避免它,什么时候不能?

Is recursion considered as a Bad Practice or not in the modern programming? When should I avoid it, and are there any circumstances when can't?

我发现的相关问题:

递归本身是一种特性吗?(不讨论好坏)
递归是否比循环更快?(答案描述递归可以带来改进在函数式语言中,但在其他语言中很昂贵),还有其他讨论性能的问题

Is recursion a feature in and of itself? (does not discuss whether it is good or bad)
Is recursion ever faster than looping? (answer describes that recursion can result in improvements in functional languages, but is expensive in others), also other questions discussing the performance

推荐答案

递归编程并不是一个坏习惯.它是您工具箱中的一个工具,就像任何工具一样,当它是唯一使用的工具时,就会发生坏事.或者当它在适当的上下文中使用时.

Recursive programming is not a bad practice. It is a tool in your toolbox and like any tool, when it's the only tool used that's when bad things happen. Or when it's used out of a proper context.

什么时候使用递归?当您有一个需要对其执行相同逻辑的树数据集时,这很好.例如:您有一个字符串元素的树列表,并且您希望计算所有字符串的总长度,沿着一个分支.您将定义一个方法来计算列表元素的长度,然后查看该元素是否有兄弟元素,以及它是否调用该方法,传递兄弟元素 - 过程重新开始.

When do you use recursion? It's good when you have a tree dataset that you need to perform the same logic upon. For example: You have a tree list of string elements and you wish to calculate total length of all strings, down one branch. You'd define a method to calculate the length of a list element and then see if the element has a sibling and if it does call the method, passing the sibling - and the process starts over.

最常见的陷阱是堆栈溢出.当列表如此之大时,它不能一次全部处理.因此,您将实施检查以确保这永远不会发生.例如,将链表分解为可管理的部分,并在您的函数中使用静态变量来跟踪您遍历的级别数 - 一旦超过该数量,就将其弃用.

The most common pitfall would be of a stack overflow. When the list is so large, it can't be handled all at once. So you would implement checks to ensure this never happens. Such as breaking the linked list down into manageable pieces and in your function, utilize a static variable to track the number of levels you traverse - bailing once that number is exceeded.

当你的数据集不是树型数据集时,你应该避免使用.我能想到的唯一一次您实际上无法避免使用递归是在不支持循环的编程语言 (Haskell) 中.

You should avoid using when your dataset is not a tree type dataset. The only time I can think of that you actually can't avoid using recursion is in programming languages that do not support looping (Haskell).

这篇关于递归通常是一种不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆