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
Option to complexity to reduce impact of switch statements #15001
Comments
Hi @Brian-Bartels, thanks for the issue! This rule aims to work per the definition of Cyclomatic complexity, so in my opinion it shouldn't have any options that would change the measuring, but I'll defer to consensus if other team members have a different opinion. |
I understand that the rule is targeting a specific definition of complexity. There are many others available that also exist such as maintainability index. This suggestion is a targeted one, to resolve one of the main drawbacks of cyclomatic complexity. I have spent the last week thinking hard about how best to enforce maintainability of a large code base that I work on. I feel like this could be an easy win to improve the trust developers have in ESLint violations. My preferred solution would be to create an aggregate rule (IE only error when a function has too many lines, and too much complexity). But I ran into too many road blocks :( |
@mdjermanovic Does my above explanation change your opinion at all? |
I think it's easy enough to create a custom rule based on the code from the core That said, I won't be opposed if other team members are in favor of adding this particular option. |
Are rules extensible? Such that I am able to tweak the functionality of complexity without completely copy pasting the whole file? |
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 For example, these are listeners in the current version of the eslint/lib/rules/complexity.js Lines 137 to 161 in 4338b74
and this is how they might look like after an upcoming change (#14957): eslint/lib/rules/complexity.js Lines 93 to 153 in 551e613
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. |
What 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:
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
The text was updated successfully, but these errors were encountered: