Partitioning and aligning long code lines

Formatting the code is very important for readability. And it is for a reason that in magazines and newspapers we have few narrow columns on the page. It is much easier to read text when you don’t have to move your eyes back and forth. However, when we code in a statically typed language like C# it is not always easy to make code narrow. Types have to be declared for every variable and parameter, so we have to break statements into several lines. Doing this in a wrong way, however, one can make more harm than good. So, let’s start considering code without breaks at all:

public class MyClass {
    public void MyMethod(LongTypeName1 longParamName1, LongTypeName2 longParamName2, LongTypeName3 longParamName3) {
        //Some logic goes here
        //Some logic goes here
        //Some logic goes here
    }
}

Reading this can be challenging. You have to move your eyes on the right and then back, distracting yourself from an ordinary top-down flow. Sometimes you even have to grab a mouse and scroll. Lame…
Here is what people start doing with this:

public class MyClass {
    public void MyMethod(LongTypeName1 longParamName1,
        LongTypeName2 longParamName2, LongTypeName3 longParamName3) {
        //Some logic goes here
        //Some logic goes here
        //Some logic goes here
    }
}

And this a mistake! Because longParamName1 is now visually detached from other parameters while logically all parameters are related and should be visually grouped together. In fact, this is the fundamental principle: logically related entities should be related visually. Now if you agree with this statement you might fix the problem in the following way:

public class MyClass {
    public void MyMethod(LongTypeName1 longParamName1,
                         LongTypeName2 longParamName2,
                         LongTypeName3 longParamName3) {
        //Some logic goes here
        //Some logic goes here
        //Some logic goes here
    }
}

Now the fundamental principle is met and we can easily see the grouping. However, there is a hidden mistake. What would happen if you rename the method? Most probably longParamName1 will be shifted left or right while all other parameters will keep their previous alignment. This change will lead to the previous problem – visual detachment of logically related entities.
This can be fixed in the following way:

public class MyClass {
    public void MyMethod(
        LongTypeName1 longParamName1, LongTypeName2 longParamName2, LongTypeName3 longParamName3) {
        //Some logic goes here
        //Some logic goes here
        //Some logic goes here
    }
}

This code solves previous visual detachment problems, but if the parameters list is long, we still have the problem. Luckily enough there is formatting which solves most of described problems:

public class MyClass {
    public void MyMethod(
        LongTypeName1 longParamName1,
        LongTypeName2 longParamName2,
        LongTypeName3 longParamName3)
    {
        //Some logic goes here
        //Some logic goes here
        //Some logic goes here
    }
}

Note that method’s opening figure bracket is placed on the new line. This visually detaches logically unrelated entities – code of the function and list of its parameters. This same rule can be applied to other things in code, to LINQ expressions for instance. It is better to format LINQ expression this way:

new[] { 1, 2, 3, 4 }
    .Where(n => n > 3)
    .OrderBy(n => n)
    .Take(3)
    .Select(n => n * n);

than this way:

new[] { 1, 2, 3, 4 }.Where(n => n > 3)
    .OrderBy(n => n)
    .Take(3)
    .Select(n => n * n);

Please refer here if you would like to learn more on the topic.

Instead of conclusion: before analyzing different formattings and selecting better one, my developer’s life was a complete mess. I spent much time formatting and reformatting things and still being unsatisfied with the visual appearance of my C# code. Now I have no hesitations and even legacy code can be a bit beautified, which is always great.

Facebooktwittergoogle_pluslinkedinmailFacebooktwittergoogle_pluslinkedinmail

Leave a Reply

Your email address will not be published.