Skip to contents

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.

Calculate the cyclomatic complexity of an R function or expression.

Usage

cyclocomp(expr)

cyclocomp_q(expr)

Arguments

expr

An R function or expression.

Value

Integer scalar, the cyclomatic complexity of the expression.

Examples

## Supply a function
cyclocomp(
  function(arg) { calulate(this); and(that) }
)
#> [1] 1
cyclocomp(ls)
#> [1] 10
cyclocomp(cyclocomp)
#> [1] 1

## Or a quoted expression
cyclocomp(quote( if (condition) "foo" else "bar" ))
#> [1] 2

## cyclocomp_q quotes the expression for you
cyclocomp_q(while (condition) { loop })
#> [1] 3

## Complexity of individual control flow constructs
cyclocomp(quote({
  if (condition) this
}))
#> [1] 2

cyclocomp(quote({
  if (condition) this else that
}))
#> [1] 2

cyclocomp(quote({
  for (var in seq) expr
}))
#> [1] 3

cyclocomp(quote({
  while (cond) expr
}))
#> [1] 3

cyclocomp(quote({
  repeat expr
}))
#> [1] 2

cyclocomp(quote({
  for (var in seq) {
    this
    break
    that
  }
}))
#> [1] 4

cyclocomp(quote({
  for (var in seq) {
    this
    next
    that
  }
}))
#> [1] 4