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
- Host: GitHub
- URL: https://github.com/MangoTheCat/cyclocomp
- Owner: MangoTheCat
- License: other
- Created: 2016-02-15T13:00:31.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-05-17T12:18:28.000Z (almost 2 years ago)
- Last Synced: 2024-04-12T00:18:31.169Z (about 1 year ago)
- Language: R
- Size: 35.2 KB
- Stars: 46
- Watchers: 9
- Forks: 7
- Open Issues: 11
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - MangoTheCat/cyclocomp - Cyclomatic complexity of R functions and expressions (R)
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
[](http://www.repostatus.org/#active)
[](https://travis-ci.org/MangoTheCat/cyclocomp)
[](https://ci.appveyor.com/project/gaborcsardi/cyclocomp)
[](http://www.r-pkg.org/pkg/cyclocomp)
[](http://www.r-pkg.org/pkg/cyclocomp)
[](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