-
Notifications
You must be signed in to change notification settings - Fork 323
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
Add rule to insert a blank line following a switch case with a multi-line body #259
Conversation
961ea92
to
369bee9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @calda . LGTM with some comments.
} | ||
} | ||
|
||
// WRONG. While the `.enableArtificialGravity` case isn't multi-line, the other cases are. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
} | ||
} | ||
|
||
// RIGHT. Since none of the cases are multi-line, blank lines are not required. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
} | ||
} | ||
|
||
// ALSO RIGHT. Blank lines are still permitted after single-line switch cases if it helps with readability. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
} | ||
} | ||
|
||
// WRONG. While it's fine to use blank lines to separate cases, spacing within a single switch statement should be consistent. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
097893d
to
18c161c
Compare
There wasn't much public engagement on this PR, but three engineers expressed support for this rule internally on Slack. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @calda !
} | ||
} | ||
``` | ||
Complex switch statements are visually busy without blank lines between the cases, making it more difficult to read the code and harder to distinguish between individual cases at a glance. Blank lines between the individual cases make complex switch statements easier to read. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Summary
This PR proposes a new rule to insert a blank line following a switch case with a multi-line body:
Autocorrect is implemented in the
blankLineAfterMultilineSwitchCase
andconsistentSwitchStatementSpacing
SwiftFormat rules, which were added in nicklockwood/SwiftFormat#1621.Reasoning
Like with declarations in a file, inserting a blank line between scopes makes them easier to visually differentiate.
Switch statements with multi-line cases can become visually busy and cramped without blank lines between the cases. In this example, all of the cases start to bleed together, making it more difficult to read the code at a glance:
Blank lines between the individual cases make the switch statement much easier to read:
Blank lines are not required in cases where all of the switch cases are a single line, but are still permitted if it helps with readability:
Please react with 👍/👎 if you agree or disagree with this proposal.