Cyclomatic Complexity of R Code
Cyclomatic complexity is a software metric (measurement), used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program’s source code. It was developed by Thomas J. McCabe, Sr. in 1976.
cyclocomp takes quoted R expressions or function objects, and returns a single integer, the cyclomatic complexity of the expression or function.
cyclocomp(
function(arg) { calulate(this); and(that) }
)
cyclocomp(ls)
cyclocomp(cyclocomp)Some more examples for the R control structures. A simple if first:
An if with an else branch:
Loops:
break and next statements add to the complexity:
Multiple (explicit or implicit) return calls also add to the complexity: