可怕的While(True)循环的最佳重构 [英] Best refactoring for the dreaded While (True) loop

查看:163
本文介绍了可怕的While(True)循环的最佳重构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果像我一样,您在While(True)循环的地点发抖,那么您也必须经过漫长而艰辛的思考,以最佳方式重构它.我已经看到了几种不同的实现,它们之间没有真正比其他任何实现更好的实现了,例如计时器&委托组合.

If, like me, you shiver at the site of a While (True) loop, then you too must have thought long and hard about the best way to refactor it away. I've seen several different implementations, none really better than any other, such as the timer & delegate combination.

那么,您提出或看到的重构可怕的While(True)循环的最佳方法是什么?

So what's the best way you've come up with or seen to refactor the dreaded While (True) loop?

编辑:如一些评论所述,我的意图是使该问题成为无限循环"重构,例如运行Windows风格的服务,其中唯一的停止条件是OnStop或致命

Edit: As some comments mentioned, my intent was for this question to be an "infinite loop" refactoring, such as running a Windows style service where the only stop conditions would be OnStop or a fatal exception.

推荐答案

我们真的需要重构 while(true)循环吗? 有时,这是一种编码标准,大多数开发人员已经习惯了这种结构.如果您必须认真思考如何重构此代码,您确定重构它是个好主意吗?

Do we really need to refactor while(true) loops? Sometimes it's a coding standard and most of the developers has got used to this structure. If you have to think hard on how to refactor this code, are you sure it's a good idea to refactor it?

Goto 曾经是编码标准中的败类.我遇到过算法,其中 goto 使代码更易读,更短.有时,不值得进行重构(或者最好使用 goto ).

Goto used to be a black sheep in coding standards. I've met algorithms where goto made the code much more readable and shorter. Sometimes it doesn't worth to refactor (or better to use goto).

另一方面,您通常可以避免 while(true).

On the other hand you can avoid while(true) most of the time.

这篇关于可怕的While(True)循环的最佳重构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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