Skip to content

Commit

Permalink
Describe issues found as complex nested instead of deeply nested (#9)
Browse files Browse the repository at this point in the history
Complexity is not about just nesting depth, there are other
considerations.

Closes #8
  • Loading branch information
scop committed Oct 31, 2021
1 parent a072451 commit 2162adb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/nakabonne/nestif)

Reports deeply 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](https://www.sonarsource.com/docs/CognitiveComplexity.pdf).
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](https://www.sonarsource.com/docs/CognitiveComplexity.pdf).

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

Expand Down
14 changes: 7 additions & 7 deletions cmd/nestif/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ func TestRun(t *testing.T) {
args: []string{"../../testdata/a.go"},
minComplexity: 1,
top: 10,
want: "../../testdata/a.go:9:2: `if b1` is deeply nested (complexity: 1)\n",
want: "../../testdata/a.go:9:2: `if b1` has complex nested blocks (complexity: 1)\n",
code: 0,
},
{
name: "show only top 2",
args: []string{"../../testdata/d.go"},
minComplexity: 1,
top: 2,
want: "../../testdata/d.go:16:2: `if b1` is deeply nested (complexity: 3)\n../../testdata/d.go:6:2: `if b1` is deeply nested (complexity: 1)\n",
want: "../../testdata/d.go:16:2: `if b1` has complex nested blocks (complexity: 3)\n../../testdata/d.go:6:2: `if b1` has complex nested blocks (complexity: 1)\n",
code: 0,
},
{
name: "show only those with complexity of 2 or more",
args: []string{"../../testdata/d.go"},
minComplexity: 2,
top: 10,
want: "../../testdata/d.go:16:2: `if b1` is deeply nested (complexity: 3)\n",
want: "../../testdata/d.go:16:2: `if b1` has complex nested blocks (complexity: 3)\n",
code: 0,
},
{
Expand All @@ -63,15 +63,15 @@ func TestRun(t *testing.T) {
args: []string{"../../testdata/a"},
minComplexity: 1,
top: 10,
want: "../../testdata/a/a.go:8:2: `if b1` is deeply nested (complexity: 1)\n",
want: "../../testdata/a/a.go:8:2: `if b1` has complex nested blocks (complexity: 1)\n",
code: 0,
},
{
name: "Check files recursively",
args: []string{"../../testdata/a/..."},
minComplexity: 1,
top: 10,
want: "../../testdata/a/a.go:8:2: `if b1` is deeply nested (complexity: 1)\n../../testdata/a/b/a.go:8:2: `if b1` is deeply nested (complexity: 1)\n",
want: "../../testdata/a/a.go:8:2: `if b1` has complex nested blocks (complexity: 1)\n../../testdata/a/b/a.go:8:2: `if b1` has complex nested blocks (complexity: 1)\n",
code: 0,
},
{
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestRun(t *testing.T) {
top: 10,
want: func() string {
path, _ := filepath.Abs("../../testdata/a/a.go")
return path + ":8:2: `if b1` is deeply nested (complexity: 1)\n"
return path + ":8:2: `if b1` has complex nested blocks (complexity: 1)\n"
}(),
code: 0,
},
Expand All @@ -109,7 +109,7 @@ func TestRun(t *testing.T) {
args: []string{"../../testdata/a.go"},
minComplexity: 1,
top: 10,
want: "[{\"Pos\":{\"Filename\":\"../../testdata/a.go\",\"Offset\":78,\"Line\":9,\"Column\":2},\"Complexity\":1,\"Message\":\"`if b1` is deeply nested (complexity: 1)\"}]\n",
want: "[{\"Pos\":{\"Filename\":\"../../testdata/a.go\",\"Offset\":78,\"Line\":9,\"Column\":2},\"Complexity\":1,\"Message\":\"`if b1` has complex nested blocks (complexity: 1)\"}]\n",
code: 0,
},
{
Expand Down
4 changes: 2 additions & 2 deletions nestif.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

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

import (
Expand Down Expand Up @@ -133,7 +133,7 @@ func (c *Checker) makeMessage(complexity int, cond ast.Expr, fset *token.FileSet
if err := p.Fprint(b, fset, cond); err != nil {
c.debug("failed to convert condition into string: %v", err)
}
return fmt.Sprintf("`if %s` is deeply nested (complexity: %d)", b.String(), complexity)
return fmt.Sprintf("`if %s` has complex nested blocks (complexity: %d)", b.String(), complexity)
}

// DebugMode makes it possible to emit debug logs.
Expand Down
8 changes: 4 additions & 4 deletions nestif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestCheck(t *testing.T) {
Column: 2,
},
Complexity: 1,
Message: "`if b1` is deeply nested (complexity: 1)",
Message: "`if b1` has complex nested blocks (complexity: 1)",
},
},
},
Expand All @@ -53,7 +53,7 @@ func TestCheck(t *testing.T) {
Column: 2,
},
Complexity: 9,
Message: "`if b1` is deeply nested (complexity: 9)",
Message: "`if b1` has complex nested blocks (complexity: 9)",
},
},
},
Expand All @@ -70,7 +70,7 @@ func TestCheck(t *testing.T) {
Column: 2,
},
Complexity: 4,
Message: "`if b1` is deeply nested (complexity: 4)",
Message: "`if b1` has complex nested blocks (complexity: 4)",
},
{
Pos: token.Position{
Expand All @@ -80,7 +80,7 @@ func TestCheck(t *testing.T) {
Column: 2,
},
Complexity: 4,
Message: "`if b1` is deeply nested (complexity: 4)",
Message: "`if b1` has complex nested blocks (complexity: 4)",
},
},
},
Expand Down

0 comments on commit 2162adb

Please sign in to comment.