nestif
1.0.0
報告複雜的IF IF GO代碼中嵌套了,通過根據G. Ann Campbell的認知複雜性白皮書定義的規則來計算其複雜性。
它可以幫助您找到是否使您的代碼難以閱讀的語句,並闡明要重構的部分。
go get github.com/nakabonne/nestif/cmd/nestif
nestif已經與Golangci-lint集成在一起。請參閱那裡的說明並啟用它。
nestif支持...操作員,以上等同於:
nestif ./...可以在一個命令中指定一個或多個文件和目錄:
nestif dir/foo.go dir2 dir3/...包裝也可以指定:
nestif github.com/foo/bar example.com/bar/baz 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
假設您寫信:
package main
func _ () {
if foo {
if bar {
}
}
if baz == "baz" {
if qux {
if quux {
}
}
}
}並給它到嵌套:
$ nestif foo.go
foo.go:9:2: `if baz == "baz"` is nested (complexity: 3)
foo.go:4:2: `if foo` is nested (complexity: 1)請注意,結果以復雜性的降序排序。此外,它僅顯示默認情況下最複雜的IF語句,您可以指定使用-top標誌顯示多少。
它根據認知複雜性的嵌套規則計算IF語句的複雜性。由於您的代碼越深狹窄,因此推理的難度越難,它會評估它的嵌套增量:
if condition1 {
if condition2 { // +1
if condition3 { // +2
if condition4 { // +3
}
}
}
} else ,如果在閱讀if時已經付費了, else if在任何地方都會增加複雜性:
if condition1 {
if condition2 { // +1
if condition3 { // +2
} else if condition4 { // +1
} else { // +1
if condition5 { // +3
}
}
}
}請參閱認知複雜性:G. Ann Campbell的一種新方法,以了解認知複雜性的更多詳細信息。