nestif
1.0.0
G. Ann CampbellによるCognitive Complenity White Paperによって定義されたルールに基づいて、その複雑さを計算することにより、GOコードの声明の場合にネストされた複雑な報告を報告します。
コードを読みやすくし、どのパーツをリファクタリングするかを明確にするステートメントを見つけるのに役立ちます。
go get github.com/nakabonne/nestif/cmd/nestif
nestifすでにGolangci-Lintと統合されています。そこの指示を参照して有効にしてください。
nestif ... GLOBオペレーターはサポートされており、上記は次のものに相当します。
nestif ./...1つ以上のファイルとディレクトリを単一のコマンドで指定できます。
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にそれを与えます:
$ nestif foo.go
foo.go:9:2: `if baz == "baz"` is nested (complexity: 3)
foo.go:4:2: `if foo` is nested (complexity: 1)結果は、複雑さの降順でソートされていることに注意してください。さらに、デフォルトでステートメントの場合、最も複雑な上位10のみを示し、 -topフラグで表示する数を指定できます。
認知の複雑さのネスティングルールに従って、IFステートメントの複雑さを計算します。コードがより深く悩まされるほど、推論するのが難しくなるので、それのネストの増加を評価します。
if condition1 {
if condition2 { // +1
if condition3 { // +2
if condition4 { // +3
}
}
}
}そうでなければ、IFを読むときに精神的なコストがすでに支払われているため、どこにelseも複雑さを増やすelse if
if condition1 {
if condition2 { // +1
if condition3 { // +2
} else if condition4 { // +1
} else { // +1
if condition5 { // +3
}
}
}
}認知的複雑さの詳細については、G。Ann Campbellによる理解可能性のホワイトペーパーを測定する新しい方法をご覧ください。