LoginSignup
3
4

More than 5 years have passed since last update.

Kotlinでdetektを使って複雑度を測る

Last updated at Posted at 2019-03-27

動機

Kotlinで雑度度(complexity)を取りたい動機がある
ただし、lizardでは未対応らしく別のものを探してみる

メトリクスをそのまま図るというよりKotlin向けのツールキットを利用するのが良さそう

detekt

Code smell analysis for your Kotlin projects らしい

サイトのFetureリストにある、
Complexity report based on logical lines of code, McCabe complexity and amount of code smells
を利用して

使い方

gradle経由で利用出来るみたいだが、コマンドラインでの利用用途が多いのでQuickStartの方法でjarを作成する

git clone https://github.com/arturbosch/detekt
cd detekt
./gradlew build shadowJar
java -jar detekt-cli/build/libs/detekt-cli-[version]-all.jar --help

helpが表示されたところでjarを実行してみる
app/src/main 以下にKotlinのファイルがあるとして -iオプションで指定してみる

java -jar detekt-cli-[version]-all.jar app/src/main
....................................................................................................................................................................................................

19 kotlin files were analyzed.

Ruleset: comments
Ruleset: complexity - 4h debt
    ComplexMethod - 12/10 - [onMeasure] at XXX.kt:32:5 
    NestedBlockDepth - 4/4 - [onMeasure] at XXX.kt:32:5
    LongMethod - 79/60 - [showDirectionalMessage] at XXX.kt:32:5
...

LongMethodなどの解析もしてくれるが、ComplexMethodを知りたいのでgrepしたりして対応してみるのが良さそう
また統計情報も出してくれるので指標として確認してみましょう(下の数値は適当です)

Complexity Report:
        - 1161 lines of code (loc)
        - 942 source lines of code (sloc)
        - 690 logical lines of code (lloc)
        - 47 comment lines of code (cloc)
        - 142 McCabe complexity (mcc)
        - 146 number of total code smells
        - 4 % comment source ratio
        - 181 mcc per 1000 lloc
        - 226 code smells per 1000 lloc

Project Statistics:
        - number of properties: 113
        - number of functions: 52
        - number of classes: 31
        - number of packages: 5
        - number of kt files: 19

lizardのAvgCCNは上記のmccをfunctionsで割った値になりそう

3
4
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
3
4