question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weird indentation when using switch-case

See original GitHub issue
switch (v) { case 1: break; case 2: break; case 3: break; default: break; }

…becomes…

switch (v) {
case 1:
    break;
case 2:
    break;
case 3:
    break;
default:
    break;
}

Maybe this is intentional, but I would expect the case to be indented by 1 level (based on the settings).

Issue Analytics

  • State:closed
  • Created 13 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
mathiasbynenscommented, Aug 2, 2010

IMHO, that really doesn’t make sense. Crockford’s reasoning to omit the extra indents is the following: “Each case is aligned with the switch. This avoids over-indentation.” ORLY?

I see your point though. If it’s by design, and you like it that way, just leave it the way it is!

4reactions
k3ncommented, Oct 11, 2012

I disagree with Crockford, with his idea it’s exceedingly more difficult to ascertain the actual ending of the switch statement. By his line of reasoning, why indent any control structures at all? They all yield an indention penalty.

I think this would be better, if one just so happened to work on an 800x600 resolution:

switch (v) {
    case 1:
    // code goes here
    break;

    default:
    // code goes here
    break;
}

Would love to have an option for this; either the “normal” way (like I have seen switch statements written my entire life), or the way I demonstrated above; anything but the default. Would you be open to a patch, if you don’t personally agree or see the need for it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Why the strange indentation on switch statements?
To put it another way, the contents of the switch block are indented one level from the switch itself, but the case s...
Read more >
Swift's SWITCH indenting is driving me mad... any way to fix ...
Cases are part of the switch statement, therefore is their indentation different from what you may be familiar with from other languages.
Read more >
Incorrect indentation in switch statements with scope inside
Format this code to reproduce (the contents of case 0 get incorrect indented, should be one indentation higher): void Foo() { int i...
Read more >
How to change indentation in "case" with curly braces?
Probably best is to settle for one of the options above, or perhaps manually fix the indentation (with Ctrl + D and similar...
Read more >
D43183 clang-format: introduce `CaseBlockIndent` to ... - LLVM
Block mode is syntaxically correct, but somewhat breaks the switch layout by moving break to different indentation levels depending on the use ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found