views:

127

answers:

4

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.

A: 

Its very suggestive.

I use the block formatting method.

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

Pierre-Alain Vigeant
I know it's subjective in part, but that's why i was hoping people would provide reasonings behind the formats.
Aren
+1  A: 

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
You have a pretty good point, definitely don't want to break a small query out into a fat formatting.
Aren
+2  A: 

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
Yeah, that was my first thought when considering block formatting, the 'wasted space' factor, but it really does increase readability.
Aren
+2  A: 

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 ununusual is happening.
David B
+1 This is what I naturally do, and for the same reasons.
jsmith