Cyclometric complexity: Difference between revisions

Content deleted Content added
Taxman (talk | contribs)
added link to complexity theory, delinked McCabe's rule, and attempted to generalize.
No edit summary
Line 1:
'''Cyclometric complexity''' is a software metric concept in [[Complexity theory]]. Its magnitude is denoted by the letter ("M"). "M" is defined to be one larger than the number of decision points (IFs, UNTILs, ENDs...) in a module ([[function]], [[procedure]], chart node, etc.), or more generally a [[system]].
 
McCabe's rule of thumb is that "modules" with M of ten or more are error-prone.
CASE structures are an exception. Due to their structure, they can have more than ten decisions without increasing the likelihood of errors.
 
The Cyclometric complexity of a section of source code is the count of the number of paths through the source code. For instance if the source code contained no decision points such as IF statements or FOR loops, the complexity would be 1 since there is only a single path through the code. If the code had a single IF statement there would be two paths through the code, one path where the IF statement is evaluated as TRUE and one path where the IF statement is evaluated as FALSE.
 
Cyclometric complexity is normally calculated by creating a graph of the source code with each line of source code being a node on the graph and arrows between the nodes showing the execution pathways. As some programming languages can be quite terse and compact, a source code statement when developing the graph may actually create several nodes in the graph (for instance when using the [[C (C_language)|C]] and [[C++ (C_Plus_Plus)|]] language tertiary If statement within a function call interface).
 
In general, in order to fully test a module all execution paths through the module should be exercised. This implies a module with a high complexity number requires more testing effort than a module with a lower value since the higher complexity number indicates more pathways through the code. This also implies that a module with higher complexity is more difficult for a programmer to understand since the programmer must understand the different pathways and the results of those pathways.
 
One would also expect that a module with higher complexity would tend to have lower [[cohesion]] (less than functional cohesion) than a module with lower complexity. The possible correlation between higher complexity measure with a lower level of cohesion is predicated on a module with more decision points generally implementing more than a single well defined function. However there are certain types modules that one would expect to have a high complexity number such as UI modules containing source code for data validation and error recovery.
 
{{msg:stub}}