BRO1524: A split conditional expression has the condition, '?' and ':' parts on their own lines¶
A conditional expression (c ? a : b) is on one line, or the condition, the ? a part and the : b part each start
their own line. Like BRO1108 for argument lists: an expression on one line is never split, but once the
line breaks next to ? or :, the other one gets a line break too. Which side of the token the line break goes on
follows BRO1520's setting, dotnet_style_operator_placement_when_wrapping (with BRO1520 off: the side the
other token's line break is on). Replaces StyleCop SA1143.
Example¶
Before¶
public class Order
{
public string Label(bool express, string name)
{
var label = express ? name + " (express)"
: name;
return label;
}
public decimal Shipping(bool express, decimal weight) =>
express
? 10m : weight * 0.5m;
}
After¶
public class Order
{
public string Label(bool express, string name)
{
var label = express
? name + " (express)"
: name;
return label;
}
public decimal Shipping(bool express, decimal weight) =>
express
? 10m
: weight * 0.5m;
}
Why¶
Each part on its own line shows at a glance where the condition ends and which value is which; a : hidden in the
middle of a line after a long ? a part is easy to miss. It's the layout most code already uses: in eight open-source
repositories, 672 of the 719 split conditionals had every part on its own line (631 with ?/: at the beginning of the
line, 41 at the end).
How the fix works¶
It inserts a line break before the ? or : that shares its line with both neighbors (after it with
dotnet_style_operator_placement_when_wrapping = end_of_line). The new line is indented like the first part of the
expression that already starts a line. A ? or : that has a line break on the wrong side is BRO1520's;
with both rules on, one dotnet format run gives the layout above.
With BRO1520 off (bro init turns it off when the code puts operators on both sides), the
line break goes on the side the other token already has its line break on, so each conditional is consistent on its
own; when the other token has line breaks on both sides, the setting decides:
// Before
var label = express ?
name + " (express)" : name;
// After
var label = express ?
name + " (express)" :
name;
Configuration¶
[*.cs]
# BRO1520's setting: beginning_of_line (default) or end_of_line.
dotnet_style_operator_placement_when_wrapping = beginning_of_line
Not reported¶
- An expression on one line, also when a part spans lines (a lambda with a block body, a wrapped argument list):
only line breaks right next to
?and:make it split. - Chains (
a ? x : b ? y : z, a conditional in another one's: bpart). Teams lay these out as a list (eachcondition ? value :on its own line, or each: condition ? valueon its own line); checking each link on its own would turn the list into a staircase. A switch expression is usually the clearer form. Conditionals nested elsewhere (in the condition or in? a, usually in parentheses) are checked on their own. - A comment or a directive next to
?or:. - A
? aor: bpart that spans lines and would move to a new line: its other lines would need re-indenting. - Code with syntax errors in the expression.