53

Before you ignore / vote-to-close this question, I consider this a valid question to ask because code clarity is an important topic of discussion, it's essential to writing maintainable code and I would greatly appreciate answers from those who have come across this before.

I've recently run into this problem, LINQ queries can get pretty nasty real quick because of the large amount of nesting.

Below are some examples of the differences in formatting that I've come up with (for the same relatively non-complex query)

No Formatting

var allInventory = system.InventorySources.Select(src => new { Inventory = src.Value.GetInventory(product.OriginalProductId, true), Region = src.Value.Region }).GroupBy(i => i.Region, i => i.Inventory);

Elevated Formatting

var allInventory = system.InventorySources
    .Select(src => 
        new { 
            Inventory = src.Value.GetInventory(product.OriginalProductId, true), 
            Region = src.Value.Region })
                .GroupBy(
                    i => i.Region, 
                    i => i.Inventory);

Block Formatting

var allInventory = system.InventorySources
    .Select(
        src => new 
        { 
            Inventory = src.Value.GetInventory(product.OriginalProductId, true), 
            Region = src.Value.Region 
        })
        .GroupBy(
            i => i.Region, 
            i => i.Inventory
        );

List Formatting

var allInventory = system.InventorySources
    .Select(src => new { Inventory = src.Value.GetInventory(product.OriginalProductId, true), Region = src.Value.Region })
    .GroupBy(i => i.Region, i => i.Inventory);

I want to come up with a standard for linq formatting so that it maximizes readability & understanding and looks clean and professional. So far I can't decide so I turn the question to the professionals here.

Aren
  • 54,668
  • 9
  • 68
  • 101
  • Take a look at ReSharper and CodeRush. They both provide automatic formatting for LINQ statements and query comprehensions. I've found they do a good job at producing a readable, comprehensible format. – LBushkin May 27 '10 at 20:09
  • 5
    I use resharper, it tends to be absolutely inconsistant in linq formatting adding wierd indentation all over. – Aren May 27 '10 at 20:13
  • 1
    Why aren't you using the linq syntax suger: (from src in system.InventorySources groupby iRegion, i.Inventory select new { Inventory = src.Value.GetInventory( product.OriginalProductID, true ), Region = src.Value.Region }); ? – TcKs May 27 '10 at 20:27
  • Great question as I have always been wondering for myself too. +1 – Kevin Le - Khnle May 27 '10 at 20:27
  • 6
    @TcKs I've found the 'SQL-like' format for LINQ is often harder to work with, it DOES look nicer, but it has problems especially when using your own Linq providers as you don't have control over the keywords you use. In this case i'm just using simple Linq-to-objects, but I also use custom linq providers in my projects. Also, the 'syntax surger' feels more like writing VB Code than C# code. – Aren May 27 '10 at 20:41

4 Answers4

27

My Formatting:

var allInventory = system.InventorySources
  .Select(src => new
  {
    Inventory = src.Value.GetInventory(product.OriginalProductId, true),
    Region = src.Value.Region
  })
  .GroupBy(
    i => i.Region,
    i => i.Inventory
  );

Notes:

  • Opening parens on methods are never worthy of a new line.
  • Closing parens match the indenting of the line that contains the opening paren.
  • The src => new stays on the same line as Select, because it's just not worthy of a new line.
  • The anonymous type always gets block treatment, just like if it was used outside of a query (but the closing paren is not worthy of a new line).
  • The two parameter GroupBy overload is not typically called. Although it could easy fit on a single line, use an extra line to make it clear that something unusual is happening.
Amy B
  • 108,202
  • 21
  • 135
  • 185
  • 1
    just like mine. but what about writing `system.InventorySources.Select(src => src.Value.Region)` Would you write it in single line or split it into multiple lines to not break your indentation standarts? That's the thing that I'm confused with lots of times :) I know it's a small thing but still... – yakya Jul 20 '17 at 12:02
  • 2
    If there's only one method call with one lambda and it's not over the desired line length, I'll leave it as a single line. – Amy B Jul 20 '17 at 16:52
  • 1
    Specifically for your third bullet, I also like this because it introduces what you're creating below to feed to the method - it doesn't belong on a new line because that would isolate it. – James D May 11 '22 at 05:33
14

I have settled on Block formatting. It bothered my sense of "wasted space" for a while but ultimately everyone felt it was more readable by more people. As we were already putting braces on new lines it just fit better with the rest of the code. There is also less room for interpretation. We keep a cs file in the public store that has formatting examples...when somebody comes up with a unique hunk of linq we add it to the file...really helps the new guys.

Rusty
  • 3,228
  • 19
  • 23
  • Yeah, that was my first thought when considering block formatting, the 'wasted space' factor, but it really does increase readability. – Aren May 27 '10 at 20:42
3

For me, it depends on the length of the query I have to make. For short simple statements like a basic select or simple joins I'll go withList Formatting because it makes it nice and easy to read without putting my code over loads of lines.

If I tend to have a rather complex or larger linq statement I go with block formatting so that it makes it easier for other people to read and follow what I was trying to do.

I don't think it's bad practice to have different formatting for different statements as long as you're consistant with how you approach it.

Richard Reddy
  • 2,120
  • 3
  • 25
  • 39
0

Its very subjective.

I use the block formatting method.

I also check the code against Stylecop and make sure that it does not yield any stylecop warning.

cordialgerm
  • 8,403
  • 5
  • 31
  • 47
Pierre-Alain Vigeant
  • 22,635
  • 8
  • 65
  • 101
  • I know it's subjective in part, but that's why i was hoping people would provide reasonings behind the formats. – Aren May 27 '10 at 20:13