Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do different coding styles indent a switch case?

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.

like image 252
Orion Avatar asked Mar 10 '18 03:03

Orion


People also ask

How do you indent codes?

Technically, it is fine to either indent using the tab key or with the space bar. Indenting once with the tab key means just pressing the tab key once. Indenting once with the space bar means pressing the space bar 4 times.

What is K & R style?

K&R style minimises the amount of vertical space which the code consumes, while maintaining that the syntax of the control structure itself does not share lines with its contents. Ensuring a control structure doesn't share lines with its content is important.

What is the meaning of indented style?

In computer programming, an indentation style is a convention governing the indentation of blocks of code to convey program structure.

Why do we indent codes?

In computer programming languages, indentation formats program source code to improve readability. Programming languages make use of indentation to define program structure . Programmers use indentation to understand the structure of their programs to human readers.


1 Answers

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...

like image 116
Orion Avatar answered Oct 16 '22 16:10

Orion