Option to complexity to reduce impact of switch statements
See original GitHub issueWhat rule do you want to change? complexity Does this change cause the rule to produce more or fewer warnings? yes How will the change be implemented? (New option, new default behavior, etc.)? A boolean option (optional) to complexity that changes switch statements from incrementing complexity by n number of cases to just incrementing it one Please provide some example code that this change will affect:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
What does the rule currently do for this code? returns a complexity of 3 What will the rule do after it’s changed? Return a complexity of 1 Are you willing to submit a pull request to implement this change? Yes
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (7 by maintainers)
Top Results From Across the Web
High cyclomatic complexity on switch statements · Issue #840
Currently, every case in a switch block increments the cyclomatic complexity regardless of whether or not it falls through.
Read more >Cyclomatic Complexity of switch case statement - Stack Overflow
The go-to answer for reducing cyclomatic complexity caused by switch statements is to convert the cases/outputs into a dictionary.
Read more >How to Reduce Cyclomatic Complexity Part 9: Switchable ...
In this series of articles we have been discussing methods that can be applied to reduce cyclomatic complexity of code.
Read more >"switch case" clauses should not have too many lines
The switch statement should be used only to clearly define some new branches in the control flow. As soon as a case clause...
Read more >This Programming Principle will Reduce if/else Statements
This Programming Principle will Reduce if/else Statements. Reduce if…else and switch statement usage by implementing better separation of concerns.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s technically possible to directly extend a core rule, but we do not recommend that approach. Rule implementations are not public API, so an extension rule that relies on the current interface of the core rule (e.g., on the set of listeners returned by
create()
) might break without notice.For example, these are listeners in the current version of the
complexity
rule: https://github.com/eslint/eslint/blob/4338b74767fa71e4e8d171f8503aa33d970e509f/lib/rules/complexity.js#L137-L161and this is how they might look like after an upcoming change (https://github.com/eslint/eslint/pull/14957): https://github.com/eslint/eslint/blob/551e613986fc462120f8363eb00099d8929ff50f/lib/rules/complexity.js#L93-L153
This change is scheduled for a major release, but the same kind of changes can happen in semver-minor or even semver-patch upgrades.
Therefore, we recommend copy & paste & modify OR copy & paste & extend from the copy. The latter approach seems like a bit more work, but it could make synchronizing the extension rule with the original rule easier.
I’m in agreement with @mdjermanovic — I don’t think it makes sense to alter the meaning of cyclomatic complexity in this rule. Creating a custom rule to cover your case is the best approach.