An open API service indexing awesome lists of open source software.

https://github.com/MangoTheCat/cyclocomp

Cyclomatic complexity of R functions and expressions
https://github.com/MangoTheCat/cyclocomp

Last synced: 5 months ago
JSON representation

Cyclomatic complexity of R functions and expressions

Awesome Lists containing this project

README

        

```{r, setup, echo = FALSE, message = FALSE}
knitr::opts_chunk$set(
comment = "#>",
tidy = FALSE,
error = FALSE,
fig.width = 8,
fig.height = 8)
```

# cyclocomp

> Cyclomatic Complexity of R Code

[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active)
[![Linux Build Status](https://travis-ci.org/MangoTheCat/cyclocomp.svg?branch=master)](https://travis-ci.org/MangoTheCat/cyclocomp)
[![Windows Build status](https://ci.appveyor.com/api/projects/status/github/MangoTheCat/cyclocomp?svg=true)](https://ci.appveyor.com/project/gaborcsardi/cyclocomp)
[![](http://www.r-pkg.org/badges/version/cyclocomp)](http://www.r-pkg.org/pkg/cyclocomp)
[![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/cyclocomp)](http://www.r-pkg.org/pkg/cyclocomp)
[![Coverage Status](https://img.shields.io/codecov/c/github/MangoTheCat/cyclocomp/master.svg)](https://codecov.io/github/MangoTheCat/cyclocomp?branch=master)

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.

## Installation

```{r eval = FALSE}
devtools::install_github("MangoTheCat/cyclocomp")
```

## Usage

```{r}
library(cyclocomp)
```

`cyclocomp` takes quoted R expressions or function objects,
and returns a single integer, the cyclomatic complexity of the
expression or function.

```{r}
cyclocomp(quote( if (condition) "foo" else "bar" ))
cyclocomp(quote( while (condition) { loop } ))
```

```{r}
cyclocomp(
function(arg) { calulate(this); and(that) }
)
cyclocomp(ls)
cyclocomp(cyclocomp)
```

Some more examples for the R control structures. A simple `if`
first:

```{r}
cyclocomp(quote({
if (condition) this
}))
```

An `if` with an `else` branch:

```{r}
cyclocomp(quote({
if (condition) this else that
}))
```

Loops:

```{r}
cyclocomp(quote({
for (var in seq) expr
}))
```

```{r}
cyclocomp(quote({
while (cond) expr
}))
```

```{r}
cyclocomp(quote({
repeat expr
}))
```

`break` and `next` statements add to the complexity:

```{r}
cyclocomp(quote({
for (var in seq) {
this
break
that
}
}))
```

```{r}
cyclocomp(quote({
for (var in seq) {
this
next
that
}
}))
```

Multiple (explicit or implicit) `return` calls also add to the
complexity:

```{r}
f <- function(arg) {
if (arg) {
return("this")
} else {
return("that")
}
"Otherwise return me"
}
cyclocomp(f)
```

## License

MIT © Mango Solutions