模塊功能
能夠同時統計代碼量和代碼復雜度
支持語言
默認遞歸分析文件夾
支持下列的一些語言等
C/C++ (works with C++14)
Java
C# (C Sharp)
JavaScript
Objective C
Swift
Python
Ruby
TTCN-3
PHP
Scala
GDScript
安裝
Git源碼鏈接 https://github.com/terryyin/lizard
pip: pip install lizard
源碼:python setup.py install --install-dir=/path/to/installation/directory/
圈復雜度
圈復雜度是 Thomas J. McCabe 在 1976年開創的軟件指標,用來判斷程序的復雜度。
這個指標度量源代碼中線性獨立的路徑或分支的數量。
根據 McCabe 所說,一個方法的復雜度最好保持在10 以下。
這是因為對人類記憶力的研究表明,人的短期記憶只能存儲 7 件事(偏差為正負 2)。
如果開發人員編寫的代碼有 50 個線性獨立的路徑,那么為了在頭腦中描繪出方法中發生的情況,需要的記憶力大約超過短期記憶容量的5倍。
簡單的方法不會超過人的短期記憶力的極限,因此更容易應付,事實證明它們的錯誤更少。
Enerjy 在 2008年所做的研究表明,在圈復雜度與錯誤數量之間有很強的相關性。
復雜度為 11 的類的出錯概率為 0.28,而復雜度為 74的類的出錯概率會上升到 0.98。
用法案例
usage: lizard [options] [PATH or FILE] [PATH] ... lizard is an extensible Cyclomatic Complexity Analyzer for many programming languages including C/C++ (doesn't require all the header files). For more information visit http://www.lizard.ws positional arguments: paths list of the filename/paths. optional arguments: -h, --help show this help message and exit --version show program's version number and exit -l LANGUAGES, --languages LANGUAGES List the programming languages you want to analyze. if left empty, it'll search for all languages it knows. `lizard -l cpp -l java`searches for C++ and Java code. The available languages are: cpp, java, csharp, javascript, python, objectivec, ttcn, ruby, php, swift, scala, GDScript, go, lua, rust, typescript 列出要分析的編程語言。如果如果留空,它將搜索它知道的所有語言。'Ligal-L CPP-L java '搜索C++和java代碼。 cpp, java, csharp,javascript, python, objectivec, ttcn, ruby, php, swift, scala, GDScript, go, lua, rust, typescript -V, --verbose Output in verbose mode (long function name) -C CCN, --CCN CCN Threshold for cyclomatic complexity number warning. The default value is 15. Functions with CCN bigger than it will generate warning 圈復雜度數警告的閾值,默認值為15,>15會產生警告。 -f INPUT_FILE, --input_file INPUT_FILE get a list of filenames from the given file 根據給出的文件獲取文件名列表 -o OUTPUT_FILE, --output_file OUTPUT_FILE Output file. The output format is inferred from the file extension (e.g. .html), unless it is explicitly specified (e.g. using --xml). 根據格式輸出到文件 -L LENGTH, --length LENGTH Threshold for maximum function length warning. The default value is 1000. Functions length bigger than it will generate warning 最大函數長度閾值警告,默認1000,超過報警 -a ARGUMENTS, --arguments ARGUMENTS Limit for number of parameters -w, --warnings_only Show warnings only, using clang/gcc's warning format for printing warnings. http://clang.llvm.org/docs/UsersManual.html#cmdoption- fdiagnostics-format 打印警告,只顯示warning,clang/gcc's格式 --warning-msvs Show warnings only, using Visual Studio's warning format for printing warnings. https://msdn.microsoft.com/en-us/library/yxkt8b26.aspx 打印警告,只顯示warning,Visual Studio's格式 -i NUMBER, --ignore_warnings NUMBER If the number of warnings is equal or less than the number, the tool will exit normally; otherwise, it will generate error. If the number is negative, the tool exits normally regardless of the number of warnings. Useful in makefile for legacy code. 如果警告數等於或小於number,則工具將正常退出;否則,它將生成錯誤。 如果數字為負數,則工具正常退出,無論警告的數量。 在遺留代碼的makefile中很有用。 -x EXCLUDE, --exclude EXCLUDE Exclude files that match the pattern. * matches everything, ? matches any single character, "./folder/*" exclude everything in the folder recursively. Multiple patterns can be specified. Don't forget to add "" around the pattern. 排除與模式匹配的文件。*匹配一切?匹配任何單個字符,“/folder/*”遞歸地排除文件夾中的所有內容。 可以指定多個模式。不要忘了在模式周圍加“”號。 -t WORKING_THREADS, --working_threads WORKING_THREADS number of working threads. The default value is 1. Using a bigger number can fully utilize the CPU and often faster. 默認使用線程數是1,大於這個數會更分的利用cpu或者運行的更快。 -X, --xml Generate XML in cppncss style instead of the tabular output. Useful to generate report in Jenkins server 生成cppncss樣式的XML而不是表格輸出。在Jenkins服務器中生成報表很有用 --csv Generate CSV output as a transform of the default output 生成CSV輸出作為默認輸出的轉換 -H, --html Output HTML report -m, --modified Calculate modified cyclomatic complexity number , which count a switch/case with multiple cases as one CCN. 計算修正的圈復雜度數,它將一個switch/case視為一個CCN。 -E EXTENSIONS, --extension EXTENSIONS User the extensions. The available extensions are: -Ecpre: it will ignore code in the #else branch. -Ewordcount: count word frequencies and generate tag cloud. -Eoutside: include the global code as one function. -EIgnoreAssert: to ignore all code in assert. -ENS: count nested control structures. 使用擴展。可用的擴展包括: -Ecpre:它將忽略#else分支中的代碼。 -Ewordcount:統計詞頻並生成標簽雲。 -Eoutside:將全局代碼作為一個函數。 -EIgnoreAssert:忽略assert中的所有代碼。 -ENS:計數嵌套控制結構。 -s SORTING, --sort SORTING Sort the warning with field. The field can be nloc, cyclomatic_complexity, token_count, p#arameter_count, etc. Or an customized field. 用字段對警告進行排序。場可以代碼行數,圈復雜度,令牌數,參數數或自定義字段。 -T THRESHOLDS, --Threshold THRESHOLDS Set the limit for a field. The field can be nloc, cyclomatic_complexity, token_count, parameter_count, etc. Or an customized file. Lizard will report warning if a function exceed the limit 設置字段的限制數。可以代碼行數,圈復雜度,令牌數,參數數或自定義字段。 如果函數設置超過了限制數會報警。 -W WHITELIST, --whitelist WHITELIST The path and file name to the whitelist file. It's './whitelizard.txt' by default. Find more information in README. 設置白名單, 默認'./whitelizard.txt'
實際執行常用命令
cd /.../...你的代碼目錄
cd /.../...你的代碼目錄
(1)lizard 默認遞歸檢測文件下的所有文件
(2)lizard -o check.txt 將所有文件輸出到某個文件
(3)lizard -C 15 檢測CCN超過15
(4)lizard -C 15 .\yingjiafupan\run_fupan.py 檢測某個文件CCN超過15
結果展示解釋
NLOC, the nloc (lines of code without comments), 不包含注釋的代碼行數
CCN, cyclomatic complexity number, 圈復雜度也就是分支復雜度,最好保持在10 以下
token, token count of functions. token的個數(關鍵字,標示符,常量,標點符號,操作符)
param, parameter count of functions. 參數統計數就是函數的參數個數
Cnt, Count的縮寫
Rt, Rate的縮寫
NLOC CCN token PARAM length location
------------------------------------------------
6 1 80 0 11 servers@14-24@.\beixiangzijin\tests\bxzj_test_api.py
2 1 7 1 2 paths@34-35@.\beixiangzijin\tests\bxzj_test_api.py
==========================================================================================
Total nloc Avg.NLOC AvgCCN Avg.token Fun Cnt Warning cnt Fun Rt nloc Rt
------------------------------------------------------------------------------------------
20506 17.1 4.3 140.0 1048 74 0.07 0.38
圈復雜度優化建議
盡量少用 if …else … ,等分支語句
每個函數要有明確的功能實現,不要為了追求行數少而合並功能實現
模型數據的賦值、校驗最好放在 model 自己里面
邏輯模塊和數據模塊要區分開編寫
常用降低圈復雜度的方法
1.采用三元表達式替換if else
2.合並條件表達式,比如使用a || b || c
3.拆分成子函數
4.去掉沒有必要的else
5.重構函數減少重復代碼