Invert condition
Negate the condition of an if-statement or conditional expression and swap its content.
Refactoring
P42 for VS Code
Why is this useful?
Negated expressions take more cognitive effort to reason about than regular expressions. Inverting negated conditions can remove unnecessary complexity. One exception is nullish comparisons where, for example, != null
indicates the existence of a value.
Mechanics
The condition of the if statement or conditional expression is negated. The then and else blocks or expressions are exchanged.
Configuration
- Code Assist ID (for the configuration file):
invert-condition
- You can configure custom keyboard shortcuts with this code action kind:
refactor.p42.invert-condition