Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arithmetic, parentheses, and null coalescing #765

Open
commonquail opened this issue Nov 25, 2022 · 2 comments
Open

Arithmetic, parentheses, and null coalescing #765

commonquail opened this issue Nov 25, 2022 · 2 comments

Comments

@commonquail
Copy link

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 Possible indentation change for array initializers #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.

@belav
Copy link
Owner

belav commented Jul 18, 2023

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 belav modified the milestones: 0.26.0, 0.27.0 Sep 8, 2023
@jods4
Copy link

jods4 commented Nov 23, 2023

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}");

@belav belav modified the milestones: 0.27.0, Planned Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants