From 2162adb2bd5cccf3dccd2ba4c830868107d81a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sun, 31 Oct 2021 09:19:03 +0200 Subject: [PATCH] Describe issues found as complex nested instead of deeply nested (#9) Complexity is not about just nesting depth, there are other considerations. Closes https://github.com/nakabonne/nestif/issues/8 --- README.md | 2 +- cmd/nestif/main_test.go | 14 +++++++------- nestif.go | 4 ++-- nestif_test.go | 8 ++++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5927752..37d3701 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/cmd/nestif/main_test.go b/cmd/nestif/main_test.go index fe426af..478dd9b 100644 --- a/cmd/nestif/main_test.go +++ b/cmd/nestif/main_test.go @@ -31,7 +31,7 @@ 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, }, { @@ -39,7 +39,7 @@ func TestRun(t *testing.T) { 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, }, { @@ -47,7 +47,7 @@ func TestRun(t *testing.T) { 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, }, { @@ -63,7 +63,7 @@ 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, }, { @@ -71,7 +71,7 @@ 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../../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, }, { @@ -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, }, @@ -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, }, { diff --git a/nestif.go b/nestif.go index 686b07b..c4bad7f 100644 --- a/nestif.go +++ b/nestif.go @@ -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 ( @@ -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. diff --git a/nestif_test.go b/nestif_test.go index e198af6..c4f8eb7 100644 --- a/nestif_test.go +++ b/nestif_test.go @@ -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)", }, }, }, @@ -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)", }, }, }, @@ -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{ @@ -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)", }, }, },