Skip to content

Latest commit

 

History

History
54 lines (37 loc) · 2.39 KB

File metadata and controls

54 lines (37 loc) · 2.39 KB

MiKo_6047: Place braces of switch expressions directly below the corresponding switch keyword

Cause

The open brace of a switch expression is not positioned directly below the switch keyword.

Rule description

Switch expressions are similar to switch statements. Therefore, the open brace of the expression should be placed directly below the switch keyword. This makes the code clearer and easier to follow.

Rationale behind

Switch expressions are a modern C# feature that provide a concise way to write switch logic. When the braces are not consistently positioned, it becomes harder to distinguish switch expressions from other constructs and understand their structure.

Placing braces directly below the switch keyword provides several important benefits:

  • Improved Visual Consistency: Aligning switch expression braces with switch statement braces creates a uniform pattern. Developers can recognize switch constructs immediately, whether they are statements or expressions.

  • Clearer Code Structure: The vertical alignment makes it easy to see where the switch expression begins and ends. This is especially helpful when switch expressions are nested or part of larger expressions.

  • Easier Code Navigation: Consistent positioning makes it easier to scan through code and identify switch expressions. Developers can quickly find switch logic without having to parse different formatting styles.

  • Better Readability: The alignment creates a clear visual hierarchy that shows the relationship between the switch keyword and its branches. This makes it easier to understand the flow of the switch expression.

  • Reduced Cognitive Load: When switch expressions follow the same formatting pattern as switch statements, developers do not need to learn different conventions. This reduces the mental effort needed to read and understand the code.

  • Enhanced Maintainability: Consistent formatting makes it easier to modify switch expressions. Developers can add or remove branches without disrupting the visual structure of the code.

How to fix violations

To fix a violation of this rule, position the open brace of the switch expression directly below the switch keyword. Ensure that the brace alignment matches the style used for switch statements.

How to suppress violations

#pragma warning disable MiKo_6047
#pragma warning restore MiKo_6047