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的一种新方法,以了解认知复杂性的更多详细信息。