Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best refactoring for the dreaded While (True) loop

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.

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

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.

like image 694
Jeff Schumacher Avatar asked Oct 28 '08 19:10

Jeff Schumacher


People also ask

What is the best approach to ensure that a code fix doesn't break?

Refactoring your code makes it easier to read and maintain. Refactoring doesn't change the external functionality of your code; it changes the way the code achieves that functionality.

When should refactoring not be done?

General logic based on this: If points 1-5 are all true, don't refactor. If any of points 2, 3, or 5 are false for multiple reasons (for example, multiple bugs would be fixed or multiple features would be easier to implement), count them as false once for each reason they are false.

When should you refactor code?

The best time to consider refactoring is before adding any updates or new features to existing code. Going back and cleaning up the current code before adding in new programming will not only improve the quality of the product itself, it will make it easier for future developers to build on the original code.


1 Answers

Why refactor? And what is so "dreadful" about this construct? It is widely used, and well understood.

If it ain't broke, don't fix it.

like image 124
Ken Ray Avatar answered Sep 30 '22 02:09

Ken Ray