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

主宰稳场 提交于 2020-02-27 04:11:16

问题


As I was writing another switch in Eclipse, I once again came across a rather weird (to me, at least) default indentation, which is applied to 'switch' statements:

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

I tend to prefer another way:

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

Which way is more readable and eye-pleasing for you? I'm still not hundred percent determined what's best for me, so I'd like to stick to what's best for other people who would read my code.

BTW, you're free to close this question if this is too subjective. :)


回答1:


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.




回答2:


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




回答3:


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.




回答4:


I use second way:

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



回答5:


The first method makes logical sense (to me), however I also prefer the second method. I think most of us are conditioned to expect that code inside braces will be indented.




回答6:


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:




回答7:


If you prefer the second case, then for consistency you should also indent the else part of an if statement:

if( test )
    do_something( );
    else
        do_something_else( );

Most people don't do this, and the convention is to keep the branches on the same level as the statement, thus Java prefers the first as the case statement's test and code branches are consistent in level than a if/then/else construct.

I've flip-flopped between them as well, but finally ended preferring the first:

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


来源:https://stackoverflow.com/questions/3005797/what-is-the-preferred-way-to-indent-cases-in-a-switch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!