Skip to content

Consider - keep Field.Method() on the same line when breaking long method chain #1010

@will-molloy

Description

@will-molloy

I came across this when working with fluent APIs, like Serilog.LoggerConfiguration and FluentAssertions.

I understand you need to insert the line breaks somewhere, but if you have Field.Method(), or Field.Field.Method() etc. it makes sense to keep them on the same line? (And only insert the line break after a method call?) Thoughts?

I guess this breaks if you have Field.Field.Field.Field... such that it overflows the line width, but I feel that would be as rare as a variable name taking up the entire width (don't have data to back that up sorry).

Few examples:


configuring Serilog logger

Expected:

        var loggerConfiguration = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .Enrich.WithProperty("key", "value")
            .Enrich.WithProperty("key", "value")
            .Enrich.WithProperty("key", "value")
            .Enrich.WithProperty("key", "value")
            .WriteTo.Console(outputTemplate: "template");

Actual:

        var loggerConfiguration = new LoggerConfiguration()
            .Enrich
            .FromLogContext()
            .Enrich
            .WithProperty("key", "value")
            .Enrich
            .WithProperty("key", "value")
            .Enrich
            .WithProperty("key", "value")
            .Enrich
            .WithProperty("key", "value")
            .WriteTo
            .Console(outputTemplate: "template");

using FluentAssertions on a List<string>

Expected:

        list.Should()
            .NotBeNull()
            .And.NotBeEmpty()
            .And.HaveCount(3)
            .And.ContainInOrder("A", "B", "C");

Actual:

        list.Should()
            .NotBeNull()
            .And
            .NotBeEmpty()
            .And
            .HaveCount(3)
            .And
            .ContainInOrder("A", "B", "C");

using IHttpContextAccessor in .NET

Expected:

        if (
            _httpContextAccessor.HttpContext != null
            && _httpContextAccessor.HttpContext.Request.Headers.TryGetValue(
                "my-header",
                out var value
            )
        )
        {
            return value;
        }

Actual:

        if (
            _httpContextAccessor.HttpContext != null
            && _httpContextAccessor
                .HttpContext
                .Request
                .Headers
                .TryGetValue("my-header", out var value)
        )
        {
            return value;
        }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions