-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Rule Request: Cognitive Complexity #3335
Comments
Yes this is great, and I'd go even further to say that a rule like this, if it were reliable, should be enabled by default. SwiftLint hasn't shied away from having opinionated defaults. |
@Nelyus FYI |
Ah right, I forgot about that configuration parameter. Thanks @lordzsolt! |
I didn't know that. Thanks for the tip ! |
Is anyone working on a PR for this rule? It doesn't appear to have ever been added to SwiftLint. |
New Issue Checklist
New rule request
the community thinks about this.
I find myself regularly disabling the "Cyclomatic complexity" rule for method that only contain a switch, and are really easy to understand.
While discussing this with fellow developers, I was told and given a link about Cognitive Complexity, which aims exactly that : replace cyclomatic complexity to provide a metric best suited to evaluate the difficulty to understand a method. It has the added benefit of evaluating types as well as methods.
I don't have a lot of experience with it by lack of current tooling to mesure it, but for my methods that currently trigger cyclomatic complexity, it makes sense.
White paper: Cognitive Complexity
Only methods having a score higher than the given threshold would trigger a violation.
The first exemple of the paper is the following 2 methods. They have the same cyclomatic complexity (4), but different cognitive complexity.
It tends to favour code structures easier to read. For exemple switch are easier to read than there if {} else if {} counterpart, it has less noise, fewer repetition of the same variables.
The rule would have 2 thresholds: the first for warnings, the second for errors.
They should have a default value, and be configurable.
In my opinion it should be enabled by default at the end of the road. I use a linter to keep my code understandable, and cognitive complexity precisely try to answer that. And at first look, it does a decent job at that.
But since it is not widely known and use, it is probably wise to have it opt-in for a while, to evaluate its relevance, and find proper default thresholds.
The text was updated successfully, but these errors were encountered: