Search code examples
cswitch-statementindentationconventionsbrackets

switch statement and K&R indentation


If we follow K&R indentation, what is the standard way to indent a case with a block { ...} inside?

Example:

switch (a) {
    case FOO: 
        f("hello"); 
        break;
    case BAR: {
        ABC abc;
        DEF def = foo(&abc);
        g(&def, &abc);
        } 
        break;
    case BAZ: 
        h(0); 
        break;
}

Note: auto-indentation by Sublime makes it like this:

switch (a) {
    case FOO: 
    f("hello"); 
    break;
    case BAR: {
        ABC abc;
        DEF def = foo(&abc);
        g(&def, &abc);
    } 
    break;
    case BAZ: 
    h(0); 
    break;
}

which seems non-optimal in readability: case and break are on the same indentation, and it's difficult to see the blocks.


Solution

  • Save time: the best indentation is to use your IDE's auto format and never use manual formatting. Assume others will auto format your code. It is simple not worth your valuable time to worry about "standard way to indent a case".