Skip to content

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. Not a StyleCop rule: proposed in StyleCop's issue #651, which the maintainers accepted and nobody built.

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.

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 : b part). Teams lay these out as a list (each condition ? value : on its own line, or each : condition ? value on 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 ? a or : b part that spans lines and would move to a new line: its other lines would need re-indenting.
  • Code with syntax errors in the expression.

Compared with StyleCop

StyleCop has no such rule; it was proposed as a readability rule in #651 (with ? and : at the beginning of the line) and never implemented. Chains were the open question there; StyleBro leaves them alone. stylebro-migrate turns this rule off, so migrating doesn't change a StyleCop-clean repository.