Deep indentation is still bad and wrong.

Tags: code formatting, indentation, pet peeve, spacing, code, style

I wrote a while back about how indenting trailing lines of code to match some semantic element of the line above was bad and wrong - it's inconsistent and hard to maintain, not to mention hard to get right initially and generally makes code harder to read unless you have a very wide screen.

Sadly, the world has failed to come around to my way of thinking. It's getting worse in C# code, since the features of the last few versions of C# encourage one to make large blocky statements. I mean variable initializer blocks and lambdas.

Look at this code that sets up a list of test data using an initializer block - the deep indentation from line 14 onwards is prime case of an initialiser block blowing out to the far right of the page.  And this code that uses an initializer block with a linq exresssion and a lambda from line 107 onwards, with a whole lot of code lined up against some right-hand edge. Line 115 to 119 are not even consitent with this format's rules, but by that point nobody is going to complain since the indetation scheme has collaped under its own absurdity.

The solution is simple: Say no to pointless, inconsistent, difficult to maintain deep indentation. Never indent code a random number of spaces to match some element of the line above. Always indent by one tab stop when the line is dependant on the line above. i.e. indent as if you had opened a '{'. It is a simpler rule, it is consitent, it gives your code one less reason to change and it is more readable.

1 Comment

  • Trevor said

    I agree to a point. Only never use tabs characters, always spaces as tabs end up getting all out of sync and screw up printing.

Add a Comment