Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+gocognit #89

Merged
merged 4 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
+ [➡ Run custom static analysis tool with `go vet`](#-run-custom-static-analysis-tool-with-go-vet)
+ [➡ Run official static analyzers not included in `go vet`](#-run-official-static-analyzers-not-included-in-go-vet)
+ [➡ Detect usafe code with `go-safer`](#-detect-usafe-code-with-go-safer)
+ [➡ Calculate cognitive complexity with `gocognit`](#-calculate-cognitive-complexity-with-gocognit)
- Code Generation
+ [➡ Run `go:generate` in parallel](#-run-gogenerate-in-parallel)
+ [➡ Generate `String` method for enum types](#-generate-string-method-for-enum-types)
Expand Down Expand Up @@ -694,6 +695,68 @@ Requirements
go install github.com/jlauinger/go-safer@latest
```

### [⏫](#contents)➡ Calculate cognitive complexity with `gocognit`

Congitive Complexity as defined in this tool can be more illustrative than Cyclometric Complexity. Research paper ["Cognitive Complexity - a new way of measuring understandability"](https://www.sonarsource.com/docs/CognitiveComplexity.pdf), 2021. — [@uudashr](https://github.com/uudashr)


```
gocognit .
```

```
// Complexity Cyclomatic=4 Cognitive=7
// Cognitive complexity give higher score compare to cyclomatic complexity.
func SumOfPrimes(max int) int { // +1
var total int
for i := 1; i < max; i++ { // +1 (cognitive +1, nesting)
for j := 2; j < i; j++ { // +1 (cognitive +2, nesting)
if i%j == 0 { // +1
continue OUT
}
}
total += i
}
return total
}

// Complexity Cyclomatic=4 Cognitive=1
// Cognitive complexity give lower score compare to cyclomatic complexity.
func GetWords(number int) string { // +1
switch number {
case 1: // +1 (cognitive 0)
return "one"
case 2: // +1 (cognitive 0)
return "a couple"
case 3: // +1 (cognitive 0)
return "a few"
default:
return "lots"
}
}
```

Example
```
$ go-binsize-treemap % gocognit .
21 main (BasicSymtabConverter).SymtabFileToTreemap basic_converter.go:23:1
12 symtab parseGoSymtabLine symtab/go_symtab_parser.go:37:1
11 main main main.go:30:1
8 symtab EqSymbolName symtab/symbol_name_parser.go:12:1
7 symtab ParseSymbolName symtab/symbol_name_parser.go:32:1
7 symtab Test_parseGoSymtabLine symtab/go_symtab_parser_private_test.go:5:1
4 symtab Test_ParseSymbolName symtab/symbol_name_parser_private_test.go:5:1
3 main updateNodeNamesWithByteSize main.go:99:1
3 main unique basic_converter.go:119:1
3 symtab (GoSymtabParser).ParseSymtab symtab/go_symtab_parser.go:14:1
2 fmtbytecount ByteCountIEC fmtbytecount/format_bytecount.go:3:1
```

Requirements
```
go install github.com/uudashr/gocognit/cmd/gocognit@latest
```

## Code Generation

### [⏫](#contents)➡ Run `go:generate` in parallel
Expand Down
9 changes: 9 additions & 0 deletions page.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ groups:
example_output_url: source/go-safer.txt
requirements:
- go install github.com/jlauinger/go-safer@latest
- title: Calculate cognitive complexity with `gocognit`
description: Congitive Complexity as defined in this tool can be more illustrative than Cyclometric Complexity. Research paper ["Cognitive Complexity - a new way of measuring understandability"](https://www.sonarsource.com/docs/CognitiveComplexity.pdf), 2021.
author: https://github.com/uudashr
commands:
- gocognit .
example_content_url: source/gocognit.txt
example_output_url: source/gocognit.out
requirements:
- go install github.com/uudashr/gocognit/cmd/gocognit@latest
- title: Code Generation
entries:
- title: Run `go:generate` in parallel
Expand Down
12 changes: 12 additions & 0 deletions source/gocognit.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$ go-binsize-treemap % gocognit .
21 main (BasicSymtabConverter).SymtabFileToTreemap basic_converter.go:23:1
12 symtab parseGoSymtabLine symtab/go_symtab_parser.go:37:1
11 main main main.go:30:1
8 symtab EqSymbolName symtab/symbol_name_parser.go:12:1
7 symtab ParseSymbolName symtab/symbol_name_parser.go:32:1
7 symtab Test_parseGoSymtabLine symtab/go_symtab_parser_private_test.go:5:1
4 symtab Test_ParseSymbolName symtab/symbol_name_parser_private_test.go:5:1
3 main updateNodeNamesWithByteSize main.go:99:1
3 main unique basic_converter.go:119:1
3 symtab (GoSymtabParser).ParseSymtab symtab/go_symtab_parser.go:14:1
2 fmtbytecount ByteCountIEC fmtbytecount/format_bytecount.go:3:1
29 changes: 29 additions & 0 deletions source/gocognit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Complexity Cyclomatic=4 Cognitive=7
// Cognitive complexity give higher score compare to cyclomatic complexity.
func SumOfPrimes(max int) int { // +1
var total int
for i := 1; i < max; i++ { // +1 (cognitive +1, nesting)
for j := 2; j < i; j++ { // +1 (cognitive +2, nesting)
if i%j == 0 { // +1
continue OUT
}
}
total += i
}
return total
}

// Complexity Cyclomatic=4 Cognitive=1
// Cognitive complexity give lower score compare to cyclomatic complexity.
func GetWords(number int) string { // +1
switch number {
case 1: // +1 (cognitive 0)
return "one"
case 2: // +1 (cognitive 0)
return "a couple"
case 3: // +1 (cognitive 0)
return "a few"
default:
return "lots"
}
}