https://github.com/gaborcsardi/cyclocomp
Cyclomatic complexity of R functions and expressions
https://github.com/gaborcsardi/cyclocomp
code-analysis complexity-measure r
Last synced: about 1 year ago
JSON representation
Cyclomatic complexity of R functions and expressions
- Host: GitHub
- URL: https://github.com/gaborcsardi/cyclocomp
- Owner: gaborcsardi
- License: other
- Created: 2023-08-30T07:35:49.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-09T13:55:30.000Z (over 1 year ago)
- Last Synced: 2025-04-14T09:12:08.923Z (about 1 year ago)
- Topics: code-analysis, complexity-measure, r
- Language: R
- Homepage:
- Size: 126 KB
- Stars: 2
- Watchers: 0
- Forks: 2
- Open Issues: 12
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
README
---
output:
github_document:
always_allow_html: yes
---
```{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
[](https://www.repostatus.org/#active)
[](https://www.r-pkg.org/pkg/cyclocomp)
[](https://www.r-pkg.org/pkg/cyclocomp)
[](https://app.codecov.io/github/Gaborcsardi/cyclocomp?branch=main)
[](https://github.com/gaborcsardi/cyclocomp/actions/workflows/R-CMD-check.yaml)
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("Gaborcsardi/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; Posit Software, PBC