30

Does line wrapping help with code readability?

Is there a generally accepted etiquette for using line continuations?

Why use this:

SomeMethod(int someInt, Object someObject,
   String someString, bool someBool)
{
    ...
}

Instead of this:

SomeMethod(int someInt, Object someObject, String someString, bool someBool)
{
    ...
}

Edit: re-worded my question from line continuation to line wrapping

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Kevin
  • 3,574
  • 10
  • 38
  • 43
  • Do you mean just spreading a statement across multiple lines, or actual continuation characters like the underscore in VB? – Aaronaught Jan 28 '10 at 02:14
  • 2
    I think this question is quite subjective, it's like asking do you like the color red? It's all about YOUR opinion. – PostMan Jan 28 '10 at 02:25
  • Agree with Postman, should add the Subjective tag. No one correct answer and everyone has their own opinion. – Ash Jan 28 '10 at 02:31
  • Pity everyone can't have my opinion, oh the world would be a better place without the color red! – PostMan Jan 28 '10 at 02:34

6 Answers6

43

Line continuations are not used in C#, since an explicit line terminator (;) is required.

If you're asking, in terms of style, whether it's a good idea to break a line into multiple lines, that's debatable. The StyleCop rules force a line to either be defined on a single line, or for every element to be on a separate line. I personally think this is a good guideline, and I usually choose to break a line completely into its parts if it's too long to fit into a good 80-90 character wide editor.


Edit in response to your new question:

In this case, I would follow the guidelines above. Personally, in your specific case, I'd leave this on one line:

SomeMethod(int someInt, Object someObject, String someString, bool someBool) 
{ 
    ... 
} 

This is a nice, short moethod declaration, and I see no reason to split it. I'd only split it if the number of arguments, and the lengths of the types, became far to long for one line of text.

If you do split it, however, I'd split it into separate lines for each argument:

SomeMethod(
    int someInt, 
    Object someObject, 
    String someString, 
    bool someBool) 
{ 
    ... 
} 

This way, at least it's obvious how and why it's split, and a developer won't accidentally skip one argument since two are on a single line.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    Agreed; I'm also a massive concise-line nerd. I can't stand automated line-wrapping. – Noon Silk Jan 28 '10 at 02:19
  • It's funny how our brains work - I'll literally be in anguish over things like this! – Eddie Groves Jan 28 '10 at 02:49
  • 3
    +1 I definitely agree with splitting all arguments or none. Inconsistency will just make it more confusing. – Kevin Kibler Jan 28 '10 at 03:19
  • 2
    I agree with this answer with one exception. I believe 120 characters is today a pretty acceptable line length when the context calls for it; however if lines are 80-90 or less, I say "well done". – Wil Moore III Dec 06 '10 at 18:52
  • 1
    @Wilmoore: I agree, unless your team has people working on laptops - VS, on a laptop especially, with reasonably sized fonts and solution explorer open, often barely hits 80 characters of width.... – Reed Copsey Dec 07 '10 at 20:53
  • @Reed: that is a fair argument indeed :) – Wil Moore III Dec 07 '10 at 23:49
  • Don't you think you need to update your thoughts about 80-90 characters per line, now that it is 2017, and bigger screens and higher resolution screens are a commonplace now? I definitely think so. – Sнаđошƒаӽ Jan 26 '17 at 10:15
22

Now that we've clarified that this isn't to do with actual line-continuation characters and simply line-wrapping - you tell me. This:

IEnumerable<int> orderIDs = context.Customers.Where(c => c.CustomerID >= 1 && c.CustomerID <= 10).Select(c => c.Orders).SelectMany(o => o).OrderBy(o => o.OrderDate).Select(o => o.OrderID);

Or this?

IEnumerable<int> orderIDs = context
    .Customers
    .Where(c => c.CustomerID >= 1 && c.CustomerID <= 10)
    .Select(c => c.Orders)
    .SelectMany(o => o)
    .OrderBy(o => o.OrderDate)
    .Select(o => o.OrderID);

Which would you rather read?

Aaronaught
  • 120,909
  • 25
  • 266
  • 342
7

I favour breaking lines at logical points. The reason is to help with source code control differencing and merging functions. I have found that understanding changes in such an environment is much easier if statements with many elements are broken onto multiple lines.

Monitors are large. But you can find youself working on a laptop computer and performing a merge in which you have base, source and target branches in separate windows across the screen. Count the characters: each of these windows on a 17-inch laptop is only about 55 characters wide.

If you are working remotely you will discover that horizontal scrolling is not well optimized, and you may well think a few reproachful thoughts about programmers who write functions with 15 parameters on a single line.

So think about ALL the ways you have to work on the source code, and break lines at places which serve ALL your needs.

Permaquid
  • 1,950
  • 1
  • 14
  • 15
  • very good points. I can see the benefit in splitting lines and using WinMerge to easily identify changes. – Kevin Jan 28 '10 at 02:41
5

I think that the limit of line length has gradually been lengthening (or disappearing) over the past few years as everyone gets widescreen hi-res monitors and rarely print out code anymore. None of the projects I've worked on have an official guideline, we just use common sense and linebreak at roughly a editor window's width (everyone uses the same basic Eclipse window layout at about the same resolution). I see no problems with this method.

Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113
  • window width. VS2K10 will have floating windows (MDI returns!) so you can float them off of your source window. I guess VB got it right. – No Refunds No Returns Jan 28 '10 at 02:30
  • @No Refunds No Returns: It seems VS drops MDI every other release, and is then put back in due to customer pressure (I guess). I definately prefer MDI editing window with all the tool windows floating and on my second monitor. – Skizz Mar 23 '11 at 09:27
2

Some years ago I read Damian Conway's Perl Best Practices. Here's a link below

Perl Best Pracices on Google Books

There's a whole chapter there dealing with Code Layout. All my feelings about code layout are summed up in his writing. I highly recommend that chapter. It can be easily applied to C#.

I've been trying to stick to his rules for whatever language I might be using : C#, Java, Perl, JavaScript, VBScript, VB, PHP,...

In this book, Conway suggest to use 78-column lines. I've got to admit I broke the rule and sticked to 80 but I think it's OK.

I've integrated this rule to every editor I use (Notepad++, Komodo, Vi, Vim, Visual Studio 2005) in order to have a visual guideline showing this limit.

Some of you might wonder how to show a guideline on VS, huh?

Well I found it to be not so obvious. In fact, you have to actually create a string parameter in the registry. Here's a post I found on SO about it. Hope it helps.

Adding a guideline to the editor in Visual Studio

Community
  • 1
  • 1
Diego Tercero
  • 1,143
  • 1
  • 11
  • 17
0

Personally, I like to be able to "see" everything. Then again, I have a pretty decent monitor. But seriously, I like nice and small functions vs. gigantic functions with nested "if" statments. Same with a lines of code.

At the end of the day, it comes down to personal preference and whatever you and your team members decide. Be consistent. And favor readability for the next guy that looks at your code.

Kris Krause
  • 7,304
  • 2
  • 23
  • 26