csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Arithmetic, parentheses, and null coalescing

Open commonquail opened this issue 2 years ago • 2 comments

This is CSharpier 0.20.0:

class X
{
    private int? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
    private int? bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
    private int? ccccccccccccccccccccccccccccccccccc;

    static void null_coalescing_arithmetic()
    {
        var x = new X();
        var result1 =
            x.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
            - x.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
            - x.ccccccccccccccccccccccccccccccccccc;
        var result2 = (
            x.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
            - x.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
            - x.ccccccccccccccccccccccccccccccccccc
        );
        var result3 =
            x.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                - x.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
                - x.ccccccccccccccccccccccccccccccccccc
            ?? 0;
        var result4 =
            (
                x.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                - x.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
                - x.ccccccccccccccccccccccccccccccccccc
            ) ?? 0;
    }
}
  • result1 and result2 each look fine in isolation (however, see my comment in #658 regarding "the rectangle rule").
  • result3 I believe follows a specific rule but appears to me inconsistent with both result1 and result2.
  • result4 is inconsistent with result2 and result3 both, but arguably consistent with result1.

result1 and result2 are semantically equivalent, as is result3 with result4.

commonquail avatar Nov 25 '22 08:11 commonquail

Probably related. Adding a null-coalescing operator caused this change in line breaking

        var discontinuedProductIds = result.GetWishListResult.GetProductCollectionResult
            ?.ProductDtos?.Where(o => o.IsDiscontinued && !o.CanBackOrder)
            .Select(o => o.Id)
            .ToHashSet();

        var discontinuedProductIds =
            result.GetWishListResult.GetProductCollectionResult
                ?.ProductDtos?.Where(o => o.IsDiscontinued && !o.CanBackOrder)
                .Select(o => o.Id)
                .ToHashSet() ?? new HashSet<Guid>();

belav avatar Jul 18 '23 14:07 belav

Another example that I think is related: null-coalesing operator at the end of a method chain:

// Without null coalescing, looks good (0.26.3)
var method = target
  .DeclaringType!
  .GetMethod(ReplaceMethod, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);

// With null coalescing: additional indentation and needless breaks (0.26.3)
var method =
  target
    .DeclaringType!
    .GetMethod(
      ReplaceMethod,
      BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public
    ) ?? throw new($"Could not find method {ReplaceMethod} on {target.DeclaringType}");

// Preferred formatting
var method = target
  .DeclaringType!
  .GetMethod(ReplaceMethod, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
  ?? throw new($"Could not find method {ReplaceMethod} on {target.DeclaringType}");

jods4 avatar Nov 23 '23 23:11 jods4