go-complexity-analysis calculates the Cyclomatic complexities, the Halstead complexities and the Maintainability indices of golang functions.
$ go get github.com/shoooooman/go-complexity-analysis/cmd/complexity
$ go vet -vettool=$(which complexity) [flags] [directory/file]
--cycloover
: show functions with the Cyclomatic complexity > N (default: 10)
--maintunder
: show functions with the Maintainability index < N (default: 20)
<filename>:<line>:<column>: func <funcname> seems to be complex (cyclomatic complexity=<cyclomatic complexity>)
<filename>:<line>:<column>: func <funcname> seems to have low maintainability (maintainability index=<maintainability index>)
$ go vet -vettool=$(which complexity) --cycloover 10 ./...
$ go vet -vettool=$(which complexity) --maintunder 20 main.go
$ go vet -vettool=$(which complexity) --cycloover 5 --maintunder 30 ./src
You can use the Github Actions to execute the complexity command on Github pull requests with reviewdog.
See shoooooman/go-complexity-analysis-action for the details.
The Cyclomatic complexity indicates the complexity of a program.
This program calculates the complexities of each function by counting idependent paths with the following rules.
Initial value: 1
+1: if, for, case, ||, &&
Calculation of each Halstead metrics can be found here.
- Comments are not considered in Halstead Metrics
- Operands and Operators are divided as follows:
- Identifiers
- Constants
- Variables
- Operators
- Parenthesis, such as "()", is counted as one operator
- Keywords
The Maintainability index represents maintainability of a program.
The value is calculated with the Cyclomatic complexity and the Halstead volume by using the following formula.
Maintainability Index = 171 - 5.2 * ln(Halstead Volume) - 0.23 * (Cyclomatic Complexity) - 16.2 * ln(Lines of Code)
This program shows normalized values instead of the original ones introduced by Microsoft.
Normalized Maintainability Index = MAX(0,(171 - 5.2 * ln(Halstead Volume) - 0.23 * (Cyclomatic Complexity) - 16.2 * ln(Lines of Code))*100 / 171)
The thresholds are as follows:
0-9 = Red
10-19 = Yellow
20-100 = Green