Who thinks it would be great if the Drupal Coding Standards included checks for Cyclomatic Complexity and maximum nesting levels?
Cyclomatic complexity is a metric used to check the complexity of a function by checking the number of paths through it. There are those that think this should be limited at 10, any more and the function is too complex and should be refactored into a number of smaller, more easy to read, functions.
A good place this has highlighted faults is with people's use of the hook_form_alter() which ends up with a switching if of doom when they should really be using hook_form_FORM_ID_alter in most cases to just work on the form they actually want to change.
Nesting levels is the number of indentations caused by a new code block.
The PHPCS Generic CoderSniffer rules include the logic for this check already and I've supplied a patch to the coder module to include if it gets included in the DCS:
http://drupal.org/node/1934594#comment-7171972
Questions:
- Should this be included in the Drupal coding standards?
- What should the cyclomatic complexity limit be? - PHPCS sets this at 10 for a warning, 20 for an error
- What should the maximum nesting level be? - PHPCS sets this at 5 for a warning and 10 for an error (quite lenient)
For more details on this metric, see: http://en.wikipedia.org/wiki/Cyclomatic_complexity