Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred way to indent cases in a switch? [closed]

People also ask

What is a quick way to indent text?

A quick way to indent is to use the Tab key. This will create a first-line indent of 1/2 inch. Place the insertion point at the very beginning of the paragraph you want to indent. Press the Tab key.

How do you correct an indent?

Select the text you want to adjust. Go to Home and select Line and Paragraph Spacing > Line Spacing Options at the bottom of the menu. The Paragraph dialog box opens. On the Indents and Spacing tab, select the options you want, and click OK.

What key can you use to help indent your code blocks accurately?

Select the lines you want to indent, and. use Ctrl + ] to indent them.


I prefer the second way as well. But it is more important to stay consistent within a particular application and/or within a particular team than it is to indent one way or the other.


According to the "official" Java Code Conventions, it's the first variant (no additional indentation for case).


I tend to indent all control structure bodies a single (4space) tab like so:

switch (i) 
{
    case 1:
        ...
    case n:
        ...
}

I consider the switch to be the outer control structure and the case directives part of the body(even though they are part of the control structure).

I would then further tab indent each case like so:

switch (i) 
{
    case 1:
        do_something();
    case n:
        do_something_else();
}

I find this to be the most readable format for the switch case construct.

As jkohlhepp has mentioned conformity to the code style conventions of the project you are working on is the most important thing, if you are working on a project that has none, it is worth developing some.


The first is the standard switch case indentation.

switch (i) {

case 1:
    .... // Here you can write more code in a row than using the second way
    break;
case n:
    ....
    break;
default:
    ....
    break;
}

Notice the new inserted line between switch (i) and case 1: