-
Notifications
You must be signed in to change notification settings - Fork 7
/
README.Rmd
133 lines (105 loc) · 2.64 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
```{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