在开关中缩进大小写的首选方法是什么?

发布于 2024-09-05 02:59:01 字数 370 浏览 4 评论 0原文

当我在 Eclipse 中编写另一个 switch 时,我再次遇到了一个相当奇怪的(至少对我来说)默认缩进,它应用于“switch”语句:

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

我倾向于更喜欢另一种方式:

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

哪种方式更具可读性和眼睛- 令你满意吗?我仍然没有百分百确定什么最适合我,所以我想坚持什么最适合其他会阅读我的代码的人。

顺便说一句,如果这个问题太主观,您可以自由地关闭这个问题。 :)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

暖心男生 2024-09-12 02:59:01

根据“官方”Java 代码约定 ,这是第一个变体(大小写没有额外的缩进)。

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

情深缘浅 2024-09-12 02:59:01

我也更喜欢第二种方式。但在特定应用程序和/或特定团队内保持一致比以某种方式缩进更重要。

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.

献世佛 2024-09-12 02:59:01

我倾向于将所有控制结构主体缩进一个(4个空格)选项卡,如下所示:

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

我认为 switch 是外部控制结构,而 case 指令是主体的一部分(即使它们是控制结构的一部分)。

然后,我将进一步缩进每个案例,如下所示:

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

我发现这是 switch case 结构最易读的格式。

正如 jkohlhepp 提到的,遵守您正在处理的项目的代码风格约定是最重要的事情,如果您正在处理的项目没有代码风格约定,那么值得开发一些。

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.

橘亓 2024-09-12 02:59:01

我用第二种方式:

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

I use second way:

switch (i) {
    case 1:
        ...
    case n:
        ...
}
爱的十字路口 2024-09-12 02:59:01

第一种方法(对我来说)具有逻辑意义,但我也更喜欢第二种方法。我认为我们大多数人都习惯于期望大括号内的代码会缩进。

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.

花桑 2024-09-12 02:59:01

第一个是标准开关盒压痕。

switch (i) {

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

请注意 switch (i) 和 case 1 之间新插入的行:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文