Best refactoring for the dreaded While (True) loop

前端 未结 12 961
感动是毒
感动是毒 2021-02-04 10:27

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 im

12条回答
  •  温柔的废话
    2021-02-04 10:48

    When I encounter a while(true) loop, that tells me either

    1. the break condition is not easily tested at the top (or bottom) of the loop,
      • there are multiple break conditions,
      • or the prior programmer was too lazy to factor the loop properly.

    1 and 2 means you might as well stick with while(true). (I use for(;;), but that's a style thing in my opinion.) I'm with another poster, why dread this? I dread tortored loops that jump through hoops to get the loop rolled "properly".

提交回复
热议问题