nestif

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2021 License: BSD-2-Clause Imports: 6 Imported by: 9

README

nestif

Go Doc

Reports complex nested if statements in Go code, by calculating its complexities based on the rules defined by the Cognitive Complexity white paper by G. Ann Campbell.

It helps you find if statements that make your code hard to read, and clarifies which parts to refactor.

Installation

By go get
go get github.com/nakabonne/nestif/cmd/nestif
By golangci-lint

nestif is already integrated with golangci-lint. Please refer to the instructions there and enable it.

Usage

Quick Start
nestif

The ... glob operator is supported, and the above is an equivalent of:

nestif ./...

One or more files and directories can be specified in a single command:

nestif dir/foo.go dir2 dir3/...

Packages can be specified as well:

nestif github.com/foo/bar example.com/bar/baz
Options
usage: nestif [<flag> ...] <Go files or directories or packages> ...
  -e, --exclude-dirs strings   regexps of directories to be excluded for checking; comma-separated list
      --json                   emit json format
      --min int                minimum complexity to show (default 1)
      --top int                show only the top N most complex if statements (default 10)
  -v, --verbose                verbose output
Example

Let's say you write:

package main

func _() {
    if foo {
        if bar {
        }
    }

    if baz == "baz" {
        if qux {
            if quux {
            }
        }
    }
}

And give it to nestif:

$ nestif foo.go
foo.go:9:2: `if baz == "baz"` is nested (complexity: 3)
foo.go:4:2: `if foo` is nested (complexity: 1)

Note that the results are sorted in descending order of complexity. In addition, it shows only the top 10 most complex if statements by default, and you can specify how many to show with -top flag.

Rules

It calculates the complexities of if statements according to the nesting rules of Cognitive Complexity. Since the more deeply-nested your code gets, the harder it can be to reason about, it assesses a nesting increment for it:

if condition1 {
    if condition2 { // +1
        if condition3 { // +2
            if condition4 { // +3
            }
        }
    }
}

else and else if increase complexity by one wherever they are because the mental cost has already been paid when reading the if:

if condition1 {
    if condition2 { // +1
        if condition3 { // +2
        } else if condition4 { // +1
	} else { // +1
	    if condition5 { // +3
	    }
        }
    }
}

Inspired by

Further reading

Please see the Cognitive Complexity: A new way of measuring understandability white paper by G. Ann Campbell for more detail on Cognitive Complexity.

Documentation

Overview

Package nestif provides an API to detect complex nested if statements.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

type Checker struct {
	// Minimum complexity to report.
	MinComplexity int
	// contains filtered or unexported fields
}

Checker represents a checker that finds nested if statements.

func (*Checker) Check

func (c *Checker) Check(f *ast.File, fset *token.FileSet) []Issue

Check inspects a single file and returns found issues.

func (*Checker) DebugMode

func (c *Checker) DebugMode(w io.Writer)

DebugMode makes it possible to emit debug logs.

type Issue

type Issue struct {
	Pos        token.Position
	Complexity int
	Message    string
}

Issue represents an issue of root if statement that has nested ifs.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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