If you break long code lines, how do you indent the stuff on the next line?

Mnementh picture Mnementh · Mar 31, 2009 · Viewed 41.9k times · Source

Sometimes you have to write in your source long lines, that are better to break. How do you indent the stuff ceated by this.

You can indent it the same:

very long
statement;
other statement;

That makes it harder to differentiate from the following code, as shown in the example. On the other hand you could indent it one level:

very long
   statement;
other statement;

That makes it easier, but it can happen, that the long line is the start of a nested block, that you want to indent, like this:

if ((long test 1) &&
   (long test 2) &&
   (long test 3)) {
   code executed if true;
}

In this case again it's hard to read. The third possibility I can think of, is to not break long lines at all, modern editors can handle it and create soft linebreaks. But with another editor you have to scroll sideways and you cannot influence the position, the editor breaks your long line.

What possibility do you prefer? Do you have other ideas to solve this? Can you support your preference with a good justification?

Answer

user53794 picture user53794 · Mar 31, 2009

I like braces on their own line because I fine it easier to see the condition and inner block all as one item (if you know what I mean):

if ((long test 1)
    && (long test 2)
    && (long test 3)) 
{
    code executed if true;
}

and I like starting additional conditional lines with what the condition is because I find that the "joining" condition is very important and it tends to get overlooked at the end of the previous line.

I also try and indent such that the effect of parenthesis are obvious (though trying to avoid long conditionals is generally a good thing).

I try and structure stuff so that I can easily "scan" for "stuff" :)