radon (python大阪勉強会 2014.10.21)
DESCRIPTION
Python大阪勉強会 2014.10.21 発表TRANSCRIPT
![Page 1: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/1.jpg)
Radon
![Page 2: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/2.jpg)
自己紹介
• 名前: 古畑耕輔
• 所属: 株式会社MonotaRO
• Pythonとの関わり
–会社がECサイトやってる
–サーバサイドは全てPython
–細々したツール類もほぼ全てPython
![Page 3: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/3.jpg)
Radonって?
• コードメトリクスを計測するツール
• 定量的な方法でコードの複雑さ、保守しやすさを計測する
• https://pypi.python.org/pypi/radon
![Page 4: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/4.jpg)
Radonで計測できるメトリクス
• Cyclomatic Complexity
• Maintainability Index
• LOC (Lines of Code)
![Page 5: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/5.jpg)
Cyclomatic Complexity
• 循環的複雑度
• Thomas McCabeという人が1976年に発表
• ifやforなど、プログラムの条件分岐が発生する事に+1を加点していく
• クラス、メソッド単位で計測
![Page 6: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/6.jpg)
Maintainability Index
• 保守容易性
• ソースコードの行数(SLOC: Source Lines Of Code)や循環的複雑度、Halstead volumeやらを元に算出
• 幾つか計算方法が発表されているが、Radon
はそれらを組み合わせた計算方法を採用している
• モジュール単位で計測
![Page 7: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/7.jpg)
計算式
![Page 8: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/8.jpg)
( ゚д゚)ポカーン
![Page 9: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/9.jpg)
試しに使ってみよう
• インストール
• コマンドライン
$ pip install radon
$ radon cc sample.py
![Page 10: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/10.jpg)
サンプルプログラム
制御構文(forやifなど)の数 + 1 = 循環的複雑度
def sample(num):for i in range(num):
if i == 0:print "Zero!"
elif i % 2 == 0:print "{} is multiples of {}".format(i, 2)
elif i % 3 == 0:print "{} is multiples of {}".format(i, 3)
else:print "Odd number"
![Page 11: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/11.jpg)
Cyclomatic Complexityの計測
$ radon cc -s sample.pysample.py
F 3:0 sample - A (5)
![Page 12: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/12.jpg)
スコアの見方
![Page 13: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/13.jpg)
オプション
![Page 14: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/14.jpg)
Maintainability Indexの計測
スコアの見方
$ radon mi -s sample.pysample.py - A (83.04)
![Page 15: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/15.jpg)
オプション
![Page 16: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/16.jpg)
よその部署のコードをチェック
• radonはお手軽にコードメトリクスを出せる
• というわけで、よその部署のコードをチェックしてみた
![Page 17: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/17.jpg)
よその部署のコードをチェック
• radonはお手軽にコードメトリクスを出せる
• というわけで、よその部署のコードをチェックしてみた
![Page 18: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/18.jpg)
yosono-busho.pyM 123:4 YosoNoBusho.__hoge - C (16)M 199:4 YosoNoBusho.__hoge - B (7)M 72:4 YosoNoBusho.execute - B (6)M 233:4 YosoNoBusho.__hoge - B (6)M 317:4 YosoNoBusho.__hoge - B (6)C 60:0 YosoNoBusho - A (5)M 264:4 YosoNoBusho.__hoge - A (3)M 281:4 YosoNoBusho.__hoge - A (2)M 295:4 YosoNoBusho.__hoge - A (2)M 353:4 YosoNoBusho.__hoge - A (2)
• B判定あるやん。C判定も。
![Page 19: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/19.jpg)
自分の部署のコードをチェックしてみた
![Page 20: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/20.jpg)
jibuntoko.pyM 145:4 MyTeam.__hoge- F (54)M 650:4 MyTeam.__hoge- D (27)M 729:4 MyTeam.__hoge- D (24)M 488:4 MyTeam.__hoge- D (22)M 101:4 MyTeam.__hoge- C (16)M 587:4 MyTeam.__hoge- C (14)M 440:4 MyTeam.__hoge- C (12)C 19:0 MyTeam - C (11)M 560:4 MyTeam.__hoge- B (9)M 32:4 MyTeam.__hoge- B (8)M 802:4 MyTeam.__hoge- B (8)
![Page 21: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/21.jpg)
完敗 orz
![Page 22: Radon (Python大阪勉強会 2014.10.21)](https://reader034.vdocuments.pub/reader034/viewer/2022042614/559457d61a28ab6a2f8b472e/html5/thumbnails/22.jpg)
ご清聴ありがとうございました