How do different coding styles indent a switch case?
Asked Answered
M

2

8

Preface: This question is not a duplicate of this one:

switch case indentation

This question is also not opinion-based. I am not seeking the "best" style. I am not asking what is the "right" thing to do.

What I am asking is how different coding styles indent switch statements, their case labels, and the actual statements.

I'm particularly interested in how a switch statement is indented in
- K&R style
- Linux kernel style
- GNU style
- Java style

My idea is to be able to be consistent in whatever code I am working with, but most indent style examples don't have switch cases. I like consistency, and the idea that what I'm writing doesn't actually match what I'm writing to is tolerable, but untasty.

Meza answered 10/3, 2018 at 3:53 Comment(6)
I figured that this question is not opinion based, because while which style is best IS opinionated, I figured WHAT a style is is not. Or am I wrong (´ u `)?Meza
You're right, this question isn't opinion based. But that's a pretty large list of styles. Generally, asking a question with such a long list of requirements is generally frowned upon (unless you also post a self-answer that answers everything, which I'd totally +1 by the way). This will likely be better received if you prune that list down to just one or two items.Trudietrudnak
I squished the list to 4 styles. While I'm fairly sure I could find the way for two of those, I don't wanna dig through the Linux kernel just to find a poorly-written example, and mistakenly take that as fact.Meza
Dear Downvoters: It would be nice if I knew why/how this question sucked, so I could write better ones in the future.Meza
The Wikipedia page on indentation styles certainly doesn't cover the niceties of C switch statements.Kevel
The whole idea is 1) not to spend too many indents. 2) maintaining the 1 indent per {} rule. And: how to indent the{ and } themselves is in most cases similar to the if/while/for brace pairs.Endopeptidase
M
4

Since the question is collecting downvotes like rainwater, I decided to find where the hell each style came from and what they said on the matter. Feel free to add. (I don't have a copy of K&R, or Whitesmiths, for example)

Java style

Specified by Oracle

www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html

switch (condition) {
case ABC:
    statements;
    /* falls through */
case DEF:
    statements;
    break;
case XYZ:
    statements;
    break;
default:
    statements;
    break;
}

Specifies a comment for whenever break is omitted.

Linux Kernel style

Used in the Linux Kernel - I hope

https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/tree/kernel/kcov.c?h=v4.15.8

switch (size) {
case 8:
        type |= KCOV_CMP_SIZE(0);
        break;
case 16:
        type |= KCOV_CMP_SIZE(1);
        break;
case 32:
        type |= KCOV_CMP_SIZE(2);
        break;
case 64:
        type |= KCOV_CMP_SIZE(3);
        break;
default:
        return;
}

I couldn't find an example for fallthroughs.

GNU style

There's a book.

https://www.gnu.org/prep/standards/standards.html

Says nothing. Looked up GNU-Emacs instead, at the suggestion of Wikipedia.

https://github.com/emacs-mirror/emacs/blob/master/src/cm.c

switch (use)
  {
  case USEHOME:
    statement;
    break;

  case USELL:
    statement;
    break;

  case USECR:
    statement;
    break;
  }

next statement;

Again, no fallthrough. As it is: in...ter...esting...

Meza answered 10/3, 2018 at 4:22 Comment(2)
For the linux kernel, here: kernel.org/doc/html/latest/process/coding-style.html the first example is a switch case with fallthrough as a function.Osteoblast
'First off, I’d suggest printing out a copy of the GNU coding standards, and NOT read it. Burn them, it’s a great symbolic gesture.' - Linus TorvaldsApodal
M
0

From his book (pg. 59):

switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
    ndigit[c-'0']++;
    break;
case ' ':
case '\n':
case '\t':
    nwhite++;
    break;
default:
    nother++;
    break;
}

A little inconsistent with the fall-throughs, but otherwise visible coding style.

Milner answered 10/12, 2020 at 4:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.