All Projects → nakabonne → nestif

nakabonne / nestif

Licence: BSD-2-Clause license
Detect deeply nested if statements in Go source code

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to nestif

golintui
A simple terminal UI for Go linters
Stars: ✭ 73 (+143.33%)
Mutual labels:  linter, static-analysis, golang-tools
constyble
CSS complexity linter
Stars: ✭ 92 (+206.67%)
Mutual labels:  linter, static-analysis, complexity
goreporter
A Golang tool that does static analysis, unit testing, code review and generate code quality report.
Stars: ✭ 3,019 (+9963.33%)
Mutual labels:  linter, static-analysis, golang-tools
Goreporter
A Golang tool that does static analysis, unit testing, code review and generate code quality report.
Stars: ✭ 2,943 (+9710%)
Mutual labels:  linter, static-analysis, golang-tools
Bodyclose
Analyzer: checks whether HTTP response body is closed and a re-use of TCP connection is not blocked.
Stars: ✭ 181 (+503.33%)
Mutual labels:  linter, static-analysis
Cflint
Static code analysis for CFML (a linter)
Stars: ✭ 156 (+420%)
Mutual labels:  linter, static-analysis
Woke
✊ Detect non-inclusive language in your source code.
Stars: ✭ 190 (+533.33%)
Mutual labels:  linter, static-analysis
Spotbugs
SpotBugs is FindBugs' successor. A tool for static analysis to look for bugs in Java code.
Stars: ✭ 2,569 (+8463.33%)
Mutual labels:  linter, static-analysis
Scopelint
scopelint checks for unpinned variables in go programs
Stars: ✭ 110 (+266.67%)
Mutual labels:  linter, golang-tools
Diktat
Strict coding standard for Kotlin and a custom set of rules for detecting code smells, code style issues and bugs
Stars: ✭ 196 (+553.33%)
Mutual labels:  linter, static-analysis
Protoc Gen Lint
A plug-in for Google's Protocol Buffers (protobufs) compiler to lint .proto files for style violations.
Stars: ✭ 221 (+636.67%)
Mutual labels:  linter, static-analysis
Rstcheck
Checks syntax of reStructuredText and code blocks nested within it
Stars: ✭ 130 (+333.33%)
Mutual labels:  linter, static-analysis
Njsscan
njsscan is a semantic aware SAST tool that can find insecure code patterns in your Node.js applications.
Stars: ✭ 128 (+326.67%)
Mutual labels:  linter, static-analysis
go-perfguard
CPU-guided performance analyzer for Go
Stars: ✭ 58 (+93.33%)
Mutual labels:  linter, static-analysis
Abaplint
Standalone linter for ABAP
Stars: ✭ 111 (+270%)
Mutual labels:  linter, static-analysis
Bellybutton
Custom Python linting through AST expressions
Stars: ✭ 196 (+553.33%)
Mutual labels:  linter, static-analysis
tryceratops
A linter to prevent exception handling antipatterns in Python (limited only for those who like dinosaurs).
Stars: ✭ 381 (+1170%)
Mutual labels:  linter, static-analysis
Revive
🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
Stars: ✭ 3,139 (+10363.33%)
Mutual labels:  linter, static-analysis
dlint
Dlint is a tool for encouraging best coding practices and helping ensure Python code is secure.
Stars: ✭ 130 (+333.33%)
Mutual labels:  linter, static-analysis
ramllint
RAML Linter
Stars: ✭ 18 (-40%)
Mutual labels:  linter, static-analysis

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].