gocyclo

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2021 License: BSD-3-Clause Imports: 9 Imported by: 1

README

Build Status codecov

Gocyclo calculates cyclomatic complexities of functions in Go source code.

The cyclomatic complexity of a function is calculated according to the following rules:

 1 is the base complexity of a function
+1 for each 'if', 'for', 'case', '&&' or '||'

To install, run

$ go get -u github.com/gregoryv/gocyclo/...

Usage:

$ gocyclo [<flag> ...] <Go file or directory> ...

Examples:

$ gocyclo .
$ gocyclo main.go
$ gocyclo -top 10 src/
$ gocyclo -over 5 *.go
$ gocyclo -avg .

The output fields for each line are:

<complexity> <package> <function> <file:row:column>

As unit test

func TestComplexity(t *testing.T) {
	files, err := filepath.Glob("*.go")
	if err != nil {
		t.Fatal(err)
	}
	max := 5
	result, ok := gocyclo.Assert(files, max)
	if !ok {
		for _, l := range result {
			t.Log(l)
		}
		t.Errorf("Exceeded maximum complexity %v", max)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Average

func Average(stats []Stat) float64

Types

type Stat

type Stat struct {
	PkgName    string
	FuncName   string
	Complexity int
	Pos        token.Position
}

func Analyze

func Analyze(paths []string) []Stat

func Assert

func Assert(paths []string, over int) (result []Stat, ok bool)

func Filter

func Filter(sortedStats []Stat, top, over int) (filtered []Stat)

func (Stat) String

func (s Stat) String() string

Directories

Path Synopsis
cmd
gocyclo
Gocyclo calculates the cyclomatic complexities of functions and methods in Go source code.
Gocyclo calculates the cyclomatic complexities of functions and methods in Go source code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL