LoginSignup
17
7

More than 5 years have passed since last update.

lizardでMcCabeの循環的複雑度を測る

Last updated at Posted at 2017-06-05

動機

Javaの循環的複雑度を測るにあたって、Eclipseプラグインを利用してきた方法がIntelliJが主な開発IDEとなった最近では気軽に実施しづらい。
計測のためだけに、Eclipse立ち上げるのもなあと思っていたところに lizardを見つけたのでメモ。

循環的複雑度

英語で言うと、Cyclomatic complexity。
簡単に言うとプログラムの複雑度を数値化したもの。
if分岐などが多いと数値が高くなるため、数値が低いほど複雑度が少ないとされる。

詳しくは Wikipediaをどうぞ
https://ja.wikipedia.org/wiki/%E5%BE%AA%E7%92%B0%E7%9A%84%E8%A4%87%E9%9B%91%E5%BA%A6

開発プロジェクトによっては「各クラスの複雑度を○○以下にすること」などと決まっていたりする。

lizard

対象言語として以下のようなものがある(抜粋)

  • Java
  • Swift
  • Objective-C
  • C/C++ (works with C++14)
  • C#
  • Scala
  • JavaScript
  • Python
  • Ruby
  • PHP
  • Golang
  • Lua

色々対応しているのでうれしい。特にSwiftに対応しているのがGood
(Kotlinへの対応は難しいらしい... https://github.com/terryyin/lizard/issues/222)

インストール

pipがインストールされていれば下記でインストール可能。

pip install lizard

実行サンプル

オプションが色々あるが、とりあえず言語を指定する -lを利用してlizard自体のコードを解析してみた。

$ lizard -l python
================================================
  NLOC    CCN   token  PARAM  length  location  
------------------------------------------------
       3      1     25      3       3 print_xml@10-12@./lizard_ext/__init__.py
       2      1     21      3       2 print_csv@15-16@./lizard_ext/__init__.py
       7      2     82      2      20 auto_open@7-26@./lizard_ext/auto_open.py
--- 中略 --- 
      11      4     62      1      11 get_extensions.expand_extensions@939-949@./lizard.py
      10      1     29      1      22 get_extensions@936-957@./lizard.py
      19      6    124      1      24 main@963-986@./lizard.py
88 file analyzed.
==============================================================
NLOC    Avg.NLOC  AvgCCN  Avg.token  function_cnt    file
--------------------------------------------------------------
     11       2.5     1.0       23.0         2     ./lizard_ext/__init__.py
     16       6.0     2.0       60.5         2     ./lizard_ext/auto_open.py
     41      24.0     6.0      103.0         1     ./lizard_ext/csvoutput.py
--- 中略 --- 
    804       8.0     2.5       49.7        89     ./lizard.py
     13       0.0     0.0        0.0         0     ./profile.py
     42       0.0     0.0        0.0         0     ./setup.py

=============================================================================================
No thresholds exceeded (cyclomatic_complexity > 15 or length > 1000 or parameter_count > 100)
==========================================================================================
Total nloc   Avg.NLOC  AvgCCN  Avg.token   Fun Cnt  Warning cnt   Fun Rt   nloc Rt
------------------------------------------------------------------------------------------
      6967       6.2     1.5       37.0      947            0      0.00    0.00

print_xml@10-12@./lizard_ext/__init__.py のように最初はメソッドごとの集計を実施
./lizard_ext/__init__.py のような次の集計はファイルごとの集計を実施

どちらも

  • NLOC:コード行数
  • CCN:循環的複雑度

は出力されている

所感

コマンドラインでの実行は慣れれば楽。
ビルドパイプラインとも結合し易いのでCIと連携して定期的にチェックすることもできそうだ。

17
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
7