Switch statement with returns — code correctness

前端 未结 17 2176
梦如初夏
梦如初夏 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:38

    I would remove them. In my book, dead code like that should be considered errors because it makes you do a double-take and ask yourself "How would I ever execute that line?"

    0 讨论(0)
  • 2020-12-12 20:38

    I would say remove them and define a default: branch.

    0 讨论(0)
  • 2020-12-12 20:39

    I would take a different tack entirely. Don't RETURN in the middle of the method/function. Instead, just put the return value in a local variable and send it at the end.

    Personally, I find the following to be more readable:

    String result = "";
    
    switch (something) {
    case 0:
      result = "blah";
      break;
    case 1:
      result = "foo";
      break;
    }
    
    return result;
    
    0 讨论(0)
  • 2020-12-12 20:39

    Exit code at one point. That provides better readability to code. Adding return statements (Multiple exits) in between will make debugging difficult .

    0 讨论(0)
  • 2020-12-12 20:42

    Interesting. The consensus from most of these answers seems to be that the redundant break statement is unnecessary clutter. On the other hand, I read the break statement in a switch as the 'closing' of a case. case blocks that don't end in a break tend to jump out at me as potential fall though bugs.

    I know that that's not how it is when there's a return instead of a break, but that's how my eyes 'read' the case blocks in a switch, so I personally would prefer that each case be paired with a break. But many compilers do complain about the break after a return being superfluous/unreachable, and apparently I seem to be in the minority anyway.

    So get rid of the break following a return.

    NB: all of this is ignoring whether violating the single entry/exit rule is a good idea or not. As far as that goes, I have an opinion that unfortunately changes depending on the circumstances...

    0 讨论(0)
  • 2020-12-12 20:42

    I personally tend to lose the breaks. Possibly one source of this habit is from programming window procedures for Windows apps:

    LRESULT WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
            case WM_SIZE:
                return sizeHandler (...);
            case WM_DESTROY:
                return destroyHandler (...);
            ...
        }
    
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    

    I personally find this approach a lot simpler, succinct and flexible than declaring a return variable set by each handler, then returning it at the end. Given this approach, the breaks are redundant and therefore should go - they serve no useful purpose (syntactically or IMO visually) and only bloat the code.

    0 讨论(0)
提交回复
热议问题