belav / csharpier

CSharpier is an opinionated code formatter for c#.
https://csharpier.com
MIT License
1.41k stars 98 forks source link

Remove prettier style method chaining #1313

Open flostellbrink opened 3 months ago

flostellbrink commented 3 months ago

I was experimenting with method chaining, and noticed that by removing GroupPrintedNodesPrettierStyle and only using GroupPrintedNodesOnLines formatting improved in my opinion.

Specifically this fixes #1298 and didn't seem to have any significant negative effects.

This seems a bit too easy, I'm sure there's a reason for the existing setup. But I thought I'd bring it up at least.

belav commented 3 months ago

I ran it on all of this code - https://github.com/belav/csharpier-repos/pull/113/files - to see some real world examples.

I think the first change in there is why this code sticks around. I believe there is a lot of EF or aspnetcore code that has a lot of Property.CallMethod().Property.CallMethod() type chains.

// current
httpResponseException = aggregateException
    .Flatten()
    .InnerExceptions.Select(ExtractHttpResponseException)
    .Where(ex => ex != null && ex.Response != null)
    .OrderByDescending(ex => ex.Response.StatusCode)
    .FirstOrDefault();

// this PR
httpResponseException = aggregateException
    .Flatten()
    .InnerExceptions
    .Select(ExtractHttpResponseException)
    .Where(ex => ex != null && ex.Response != null)
    .OrderByDescending(ex => ex.Response.StatusCode)
    .FirstOrDefault();

I do have a ticket somewhere, to prefer keeping method calls together. Which this does kind of accomplish. Perhap the logic for when to use the prettier style grouping just needs to be adjusted.


// current
IExtensibleModelBinder childBinder = bindingContext.ModelBinderProviders.GetBinder(
    controllerContext,
    innerBindingContext
);

// this pr
 IExtensibleModelBinder childBinder = bindingContext
    .ModelBinderProviders
    .GetBinder(controllerContext, innerBindingContext);

// maybe should try to keep properties together like this
 IExtensibleModelBinder childBinder = bindingContext.ModelBinderProviders
    .GetBinder(controllerContext, innerBindingContext);