Skip to content
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

Additional Option for Cyclomatic Complexity #1220

Closed
jasonmk opened this issue Jul 21, 2014 · 5 comments
Closed

Additional Option for Cyclomatic Complexity #1220

jasonmk opened this issue Jul 21, 2014 · 5 comments

Comments

@jasonmk
Copy link

jasonmk commented Jul 21, 2014

I'd like to see an option for Cyclomatic Complexity that allows me to ignore 'when' statements when computing the complexity of a method. Basically, any method with a case statement is virtually guaranteed to exceed the Complexity limit. If I only had 1 or two possible conditions, I'd probably be using if/elsif/else anyways. Case statements are great for a method that branches off into a number of different paths, but that doesn't make it inherently complex.

@jonas054
Copy link
Collaborator

@jasonmk I think that, broadly speaking, cyclomatic complexity is a metric that tells you how difficult is this to test. What you're looking for is slightly different, how difficult is this to read. So if we're going to add this configuration option, it's probably best to create a new cop for it, because without the whens, the metric isn't really cyclomatic complexity.

@bbatsov @yujinakayama What do you think about adding a Metrics namespace? Cops such as ClassLength, MethodLength, and CyclomaticComplexity could be moved there.

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 22, 2014

👍 for Metrics namespace.

@jasonmk
Copy link
Author

jasonmk commented Jul 22, 2014

Awesome, that sounds great.

@yujinakayama
Copy link
Collaborator

👍 for Metrics namespace too.

jonas054 added a commit to jonas054/rubocop that referenced this issue Jul 29, 2014
Move the Style cops BlockNesting, ClassLength,
CyclomaticComplexity, MetricsLineLength, MethodLength,
and ParameterLists to the new namespace.
@dpashk-figma
Copy link

Just want to add my two cents. While it's true that the classic cyclomatic complexity is a strict metric of testability, practical applications have led to the creation of its variant, modified complexity.

Modified cyclomatic complexity is almost exactly the same as the classic cyclomatic complexity, except that a switch statement only increases the value by 1 regardless of how many case statements it contains.

A number of other code analysis tools implement this metric. See:

Given that it's common enough in the industry, I propose that you still consider adding an option to the existing cyclomatic complexity rule, or accepting a PR for a separate Modified Complexity rule (it'll probably just inherit from the existing Cyclomatic Complexity rule). Note that this is still different from the PerceivedComplexity rule that now exists.

I'd be happy to make a contribution either way. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants