New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/hochata/cyclomatic

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/hochata/cyclomatic

  • v0.0.0-20220905230438-6c9c05e32915
  • Source
  • Go
  • Socket score

Version published
Created
Source

cyclomatic

Go cyclomatic complexity command and analysis.Analyzer.

It is important to keep your functions simple and error-free. cyclomatic lets you know when your functions get too complex and error-prone.

As a command

First, install cyclomatic command via go get.

$ go get github.com/ichiban/cyclomatic/cmd/cyclomatic

Then, run cyclomatic command at your package directory.

$ cyclomatic ./...
/Users/ichiban/src/example.com/package/foo.go:100:1: cyclomatic complexity of example.com/package.Foo exceeded limit 13 > 10
/Users/ichiban/src/example.com/package/foo.go:200:1: cyclomatic complexity of example.com/package.Bar exceeded limit 22 > 10

Also, you can relax the limit with -limit flag.

$ cyclomatic -limit 15 ./...
/Users/ichiban/src/example.com/package/foo.go:200:1: cyclomatic complexity of example.com/package.Bar exceeded limit 22 > 15

As an analysis.Analyzer

First, install the package via go get.

$ go get github.com/ichiban/cyclomatic

Then, include cyclomatic.Analyzer in your checker.

package main

import (
	"golang.org/x/tools/go/analysis"
	"golang.org/x/tools/go/analysis/multichecker"
	"golang.org/x/tools/go/analysis/passes/nilfunc"
	"golang.org/x/tools/go/analysis/passes/printf"
	"golang.org/x/tools/go/analysis/passes/shift"

	"github.com/ichiban/cyclomatic"
)

func main() {
	multichecker.Main(
		// other analyzers of your choice
		nilfunc.Analyzer,
		printf.Analyzer,
		shift.Analyzer,

		cyclomatic.Analyzer,
	)
}

Definition of complexity

For each function definition:

  • The base complexity is 1
  • When we see if or for, increment the complexity
  • When we see && or ||, increment the complexity
  • When we see switch or select, increase the complexity by the number of values listed after cases

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

This package is based on these packages but slightly differs on how to count complexity:

FAQs

Package last updated on 05 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc