Should I avoid using Java Label Statements?

前端 未结 11 1473
北荒
北荒 2020-11-29 04:27

Today I had a coworker suggest I refactor my code to use a label statement to control flow through 2 nested for loops I had created. I\'ve never used them before because per

11条回答
  •  情书的邮戳
    2020-11-29 04:59

    I'd argue in favour of them in some locations, I found them particularly useful in this example:

    
    nextItem: for(CartItem item : user.getCart()) {
    
      nextCondition : for(PurchaseCondition cond : item.getConditions()) {
         if(!cond.check())
            continue nextItem;
         else
            continue nextCondition;
    
      }
      purchasedItems.add(item);
    }
    

提交回复
热议问题