Files
codeql/javascript/ql/src/Metrics/FunCyclomaticComplexity.qhelp
2018-08-02 17:53:23 +01:00

43 lines
1.7 KiB
XML

<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>This metric measures the cyclomatic complexity of each function in the project.</p>
<p>The cyclomatic complexity of a function is an indication of the number of paths that can be taken during
the execution of a function. Code with many branches and loops has high cyclomatic complexity. A cyclomatic
complexity above 50 should be considered bad practice and above 75 should definitely be addressed.</p>
<p>Functions with high cyclomatic complexity are</p>
<ul>
<li>difficult to test since tests should be provided for each possible execution path;</li>
<li>difficult to understand since a developer needs to understand how all conditions interact;</li>
<li>difficult to maintain since many execution paths is an indication of functions that perform too many tasks.</li>
</ul>
</overview>
<recommendation>
<p>The primary way to reduce the complexity is to extract sub-functionality into separate functions.
This improves on all problems described above. If the function naturally breaks up into a sequence of operations it
is preferable to extract each operation as a separate function. Even if that's not the case it is often possible
to extract the body of an iteration into a separate function to reduce complexity. If the complexity can't be
reduced significantly make sure that the function is properly documented and carefully tested.</p>
</recommendation>
<references>
<li>
M. Fowler. <em>Refactoring</em>. Addison-Wesley, 1999.
</li>
<li>
T. J. McCabe. <em>A Complexity Measure</em>. IEEE Transactions on Software Engineering, SE-2(4), December 1976.
</li>
<li>
Wikipedia: <a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic complexity</a>.
</li>
</references>
</qhelp>