In Visual Studio using C# (this happens for me in both 2010 and 2012), I get a weird auto-indent if I enter a new line before an else statement:
if (condition1)
{
DoSomething();
} // press Enter here, after the brace
// auto indents to here
// Helpful comment here about condition2, but awkwardly auto-indented
else if (condition2)
{
DoSomethingElse();
}
Intuitively, I would expect it to only auto-indent to the same column as the if/else, but it instead goes one indent-level further.
I realize it’s illegal to put other code in between those two blocks anyhow, but I’d still like to put comments there. Does anyone know why this happens or how to fix it? It’s so incredibly weird and annoying to have to hit backspace 4 times whenever I try to comment an else block.
I know someone’s going to say “just put the comment inside the else/if block”, but I’m interested in an actual explanation/solution of/to this phenomenon; furthermore, it doesn’t make sense to me to have a comment explaining code that happened two lines ago.
I also know someone’s going to say “just put the comment on the same line as the else/if block”, but putting comments and code on the same line seems yucky to me.
I just tried this in Visual Studio 2012:
Indeed, when you write the comment, it doesn’t seem correctly indented.
But after pressing CTRL + E, CTRL + D, everything is automatically indented correctly (in my opinion):