How and where to break long lines of code

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Possible Duplicates:
http://stackoverflow.com/questions/283465/where-to-wrap-a-line-of-code-especially-long-argument-lists http://stackoverflow.com/questions/283465/where-to-wrap-a-line-of-code-especially-long-argument-lists http://stackoverflow.com/questions/566082/coding-standards-and-line-length http://stackoverflow.com/questions/566082/coding-standards-and-line-length


Hi,

    • Where do you break long lines of code? (Example: before or after an operator)
    • If it is a long string, do you split the string into substrings?
    • What other cases exist when breaking long lines?

Answers

This is personal preference only since the question is a subjective one, but I tend to follow these guidelines.

    • Limit code to 79 characters where possible (this is just so it prints out nicely at a high enough font size for my aging eyes to read it). Usually I ll break them initially somewhere in the 60-range so that the inevitable editing of lines to fix problems doesn t mean I have to reformat a lot :-)
    • Break the line as high up in the hierarchy as possible. By that I mean x = 1 * 2 + 3 * (4 + 5); would be broken at the first + in preference to anywhere else. That s because the highest level "elements" of the statement are x, 1 * 2 and 3 * (4 + 5).
    • Use indentation intelligently to ensure that the following lines are seen as part of the first line. For example, if the first line of a three line statement starts with 3 tabs, the following two lines have 4 tabs.

Something like:

x = 1 * 2 +
    3 * (4 + 5);

or:

if (1 * 2 =
    3 * (4 + 5))
{
    doSomething();
}

That last one is usually the only time I put an opening brace on a separate line. Usually I would have:

if (1 * 2 = 3 * (4 + 5)) {
    doSomething();
}

but splitting it makes it look like the second line of the if is part of the block:

if (1 * 2 =
    3 * (4 + 5)) {
    doSomething();
}

which I don t like.

As for strings, where they are longer than my preferred width, I tend to break them up. C and its brethren make this easy since the compiler treats "abc" "def" exactly the same as "abcdef". Even if there is a runtime impact (like concatenating strings), I ll break them up but I ll still try to minimise such impacts.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/3137778/how-and-where-to-break-long-lines-of-code

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils