Files
codeql/cpp/ql/src/Architecture/Refactoring Opportunities/CyclomaticComplexity.qhelp
2018-08-02 17:53:23 +01:00

31 lines
1.5 KiB
XML

<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>This rule computes cyclomatic complexity per function. Cyclomatic complexity is a measure of how complex a function
is, and is derived from the number of branching statements in the function. Complex functions can be difficult to understand
and maintain, and usually can be split into smaller, less complex functions.</p>
<p>The rule finds functions with high (e.g. >50) cyclomatic complexity.</p>
</overview>
<recommendation>
<p>With increasing cyclomatic complexity there need to be more test cases that are necessary to achieve a complete branch coverage when testing the function.
Try to reduce the function's complexity by splitting it into several more cohesive functions.
A particularly effective way of reducing complexity is to put code that handles a particular case in an large <code>if</code> or
<code>switch</code> statement into a separate function with a descriptive name. This not only reduces the complexity of the function,
but makes it considerably more readable as the function's descriptive name gives an idea of its purpose without the developer
analyzing each line of code.
</p>
</recommendation>
<references>
<li>Wikipedia: <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic complexity</a>.</li>
<li>T. McCabe. <em>A Complexity Measure</em>. ICSE '76: Proceedings of the 2nd international conference on Software engineering, 1976.</li>
</references>
</qhelp>