-
Notifications
You must be signed in to change notification settings - Fork 363
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
Add new check for nesting level #517
Add new check for nesting level #517
Conversation
0c2d392
to
96b1c7f
Compare
What is the difference to cyclomatic complexity (http://en.m.wikipedia.org/wiki/Cyclomatic_complexity)? |
They measure different things based on same concept? For example you can say you should only 5 levels of nesting in a function. And the complexity can still not flag any violation because its 10. Complexity gives the idea as if the function is only with one nested level. |
Yeah, they are related, but cyclomatic complexity is more of a flat measure, e.g. lots of sequential unrelated I think the nesting level is more indicative of a need for refactoring, but also easier refactoring. Pulling a deeply-nested block out to a separate function is easier and usually conceptually 'better' than splitting any general high-complexity method. This PR does have one subtle point that may need consideration. In the AST, the way descendent nodes of
are actually equivalent to this:
So a series of else-ifs will increase the nesting level by one each time. But I think this is standard behaviour for most C++ static analysers (I recall one of my colleagues coded a rule for cppcheck which found the same thing). However it would also be possible to catch the differing cases of an Perhaps I should make this alternative behaviour a parameter for the check? |
If this metric is really measuring the nesting level (second case above) it makes sense. Would add no parameter because first case is equal to cyclomatic complexity. Especially with 'if/else if' and 'switch/case' the cyclomatic complexity metric is mostly wrong. |
Using a method inspired by Sonar-Java, check code for the depth of nested blocks and raise issues accordingly.
96b1c7f
to
bb1a0ac
Compare
Okay, the if-else behaviour has been rectified. The test case is reasonably comprehensive, but please do let me know if there are any glaring errors or omissions in there. |
Add new check for nesting level
Using a method inspired by Sonar-Java, check code for the depth of
nested blocks and raise issues accordingly.