views:

151

answers:

5

Hey there,

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

A: 

I use second way:

switch (i) {
    case 1:
        ...
    case n:
        ...
}
Upul
+7  A: 

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.

jkohlhepp
+1  A: 

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.

DRL
+1 I too place the opening brace on a new line.
onedaywhen
uggg, the open bracket of a block should go on the same line as the block definition.
Jay
A: 

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.

virtualsue
A: 

According to the "official" Java Code Conventions, it's the first variant (no additional indentation for case): http://java.sun.com/docs/codeconv/html/CodeConventions.doc6.html#468

Chris Lercher