Switch statement with returns — code correctness

前端 未结 17 2219
梦如初夏
梦如初夏 2020-12-12 20:05

Let\'s say I have code in C with approximately this structure:

switch (something)
{
    case 0:
      return \"blah\";
      break;

    case 1:
    case 4:
         


        
17条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 20:49

    Keep the breaks - you're less likely to run into trouble if/when you edit the code later if the breaks are already in place.

    Having said that, it's considered by many (including me) to be bad practice to return from the middle of a function. Ideally a function should have one entry point and one exit point.

提交回复
热议问题