Skip to content

Calculate complexities of golang functions with static analysis

Notifications You must be signed in to change notification settings

shoooooman/go-complexity-analysis

Repository files navigation

go-complexity-analysis

go-complexity-analysis calculates the Cyclomatic complexities, the Halstead complexities and the Maintainability indices of golang functions.

Install

$ go get github.com/shoooooman/go-complexity-analysis/cmd/complexity

Usage

$ go vet -vettool=$(which complexity) [flags] [directory/file]

Flags

--cycloover: show functions with the Cyclomatic complexity > N (default: 10)

--maintunder: show functions with the Maintainability index < N (default: 20)

Output

<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>)

Examples

$ 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

Github Actions

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.

Metrics

Cyclomatic Complexity

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, ||, &&

Halstead Metrics

Calculation of each Halstead metrics can be found here.

Rules

  1. Comments are not considered in Halstead Metrics
  2. Operands and Operators are divided as follows:

Operands

  • Identifiers
  • Constants
  • Variables

Operators

  • Operators
    • Parenthesis, such as "()", is counted as one operator
  • Keywords

Maintainability Index

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

About

Calculate complexities of golang functions with static analysis

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages