golongfuncs是通過各種度量(以及度量組合)搜索“長”功能的工具。
此工具可以幫助您回答問題線:
換句話說,它將幫助您從距離長時間的長度功能中過濾漫長而復雜的功能。
go install github.com/tkrajina/golongfuncs/...
該工具可以計算以下功能“長度”和“複雜性”測量:
lines :沒有空行的行數,線結束塊(僅包含} )和註釋,total_lines :線數,包括空行和註釋,len :功能中的字符數(沒有註釋和空行)。total_len :功能中的字符數(帶有註釋和空行)。comments :評論數。多琳評論一次,一次comment_lines :評論行的數量,complexity :循環複雜性(來自Gocyclo),max_nesting :最大嵌套塊深度(注意,構造初始化),total_nesting :Total嵌套(換句話說,如果代碼的格式正確 - 每個縮進選項卡都被計數一次)in_params :函數輸入參數的數量out_params :功能輸出參數的數量variables :函數範圍中的變量數(沒有函數參數和函數接收器)assignments :函數中的作業數(包括變量聲明, := , = , += , -= ...)control :控制流語句的數量( if是else , switch , case default select和defer )todos :在註釋中找到的數字“ todo”標籤(包括功能主體和函數之前的表彰)。標籤是HACK , TODO , NOTE , FIXME , ASAP , ISSUE , BUG和WTF 。除此之外,您還可以結合措施。例如:
complexity/lines :計算每行代碼的平均複雜度。total_nesting/total_lines :計算每行平均嵌套(縮進)。comment_lines/total_lines :計算每行函數行。計算您的Golang代碼的任何措施:
$ golongfuncs +variable
$ golongfuncs +total_nesting
$ golongfuncs +lines
$ golongfuncs +lines,complexity
$ golongfuncs +lines,complexity,complexity/lines
僅呼叫golongfuncs (無參數)是golongfuncs +lines的別名。
$ golongfuncs [flags] +[types] [paths...]
找到最長的功能:
$ golongfuncs <go_file>
$ golongfuncs <directory>
$ golongfuncs <directory>/...
如果未指定路徑,則GolongFuncs假定是./...
顯示多種措施:
$ golongfuncs +lines
$ golongfuncs +lines,in_params
$ golongfuncs +lines,in_params,complexity/lines
$ golongfuncs +lines +in_params +complexity/lines
如果指定了多個度量,則將結果通過第一列(在此示例lines中)對:
默認情況下,結果僅顯示前20個結果。用-top更改它:
$ golongfuncs -top 50
默認情況下,功能短於10行。您可以使用-min-lines <n>更改它。
100個最複雜的功能:
$ golongfuncs -top 100 +complexity ./...
最複雜的功能超過50行:
$ golongfuncs -min-lines 50 +complexity ./...
找到長功能,但還計算出它們的複雜性,AVG複雜性和AVG嵌套:
$ golongfuncs +lines,complexity,complexity/lines,total_nesting/total_lines .
ExampleVeryLongfunction golongfuncs/runner_test.go:118:1 lines=305.0 complexity=1.0 complexity/lines=0.1 total_nesting/total_lines=1.0
ExampleVeryComplexFunction golongfuncs/runner_test.go:10:1 lines=69.0 complexity=44.0 complexity/lines=0.6 total_nesting/total_lines=6.7
printStats main.go:54:1 lines=21.0 complexity=9.0 complexity/lines=0.4 total_nesting/total_lines=2.5
main main.go:12:1 lines=19.0 complexity=3.0 complexity/lines=0.2 total_nesting/total_lines=1.0
TestLines golongfuncs/runner_test.go:476:1 lines=15.0 complexity=2.0 complexity/lines=0.1 total_nesting/total_lines=0.9
NewVisitor golongfuncs/runner.go:94:1 lines=15.0 complexity=3.0 complexity/lines=0.2 total_nesting/total_lines=1.0
Get golongfuncs/models.go:34:1 lines=15.0 complexity=7.0 complexity/lines=0.5 total_nesting/total_lines=1.7
TestNesting golongfuncs/runner_test.go:438:1 lines=15.0 complexity=2.0 complexity/lines=0.1 total_nesting/total_lines=0.9
您可以看到ExampleVeryLongfunction功能很長(305行),但平均複雜性較低(0.1),而AVG嵌套為1.0。 AVG嵌套1.0意味著此功能中沒有嵌套塊。如果一半的線在一個嵌套的塊中(例如, if <expr> { ...code... }代碼的塊)AVG嵌套為1.5。
ExampleVeryComplexFunction較短(69行),但平均複雜性(每行代碼)為0.6和AVG嵌套6.7,這可能是一個很好的提示,即功能需要進行重構。
查找超過5行的功能,其中AVG嵌套(每行代碼)大於5,其中包括總線數和行計數:
$ golongfuncs +total_nesting/total_lines,total_lines,lines -threshold 5 .
ExampleVeryComplexFunction golongfuncs/runner_test.go:10:1 total_nesting/total_lines=6.7 total_lines=108.0 lines=69.0
查找平均線長度最長的功能:
$ golongfuncs +len/lines ./...
$ golongfuncs +total_len/total_lines ./...
測試和供應商的文件被忽略,如果要測量它們,請使用-include-tests和-include-vendor 。
任意文件/目錄可以用-ignore "<regexp>"忽略。例如,如果您想-ignore "^.*_generated.go$"包含_generated.go Golang文件。
可以使用-ignore-func "regexp"忽略功能。
此工具已根據Apache許可證(版本2.0)許可