csharpier
csharpier copied to clipboard
Arithmetic, parentheses, and null coalescing
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
andresult2
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 bothresult1
andresult2
. -
result4
is inconsistent withresult2
andresult3
both, but arguably consistent withresult1
.
result1
and result2
are semantically equivalent, as is result3
with result4
.
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>();
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}");