Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Cognitive Complexity でコードの複雑さを定量的に計測しよう
Search
s2terminal
June 09, 2019
Technology
2
110
Cognitive Complexity でコードの複雑さを定量的に計測しよう
s2terminal
June 09, 2019
Tweet
Share
More Decks by s2terminal
See All by s2terminal
AIをWebアプリに実装するための便利なPythonライブラリ
s2terminal
0
480
NiceGUI is Nice
s2terminal
0
490
段階的なシステムリプレースを実現するデータ同期技術
s2terminal
0
87
1年でモダンなフロントエンドに追いついた話 2019-08-22 Mix Leap Joint #26
s2terminal
0
35
20190706 BCU30 事業を変えるシステムリプレース
s2terminal
0
27
MySQLオンラインマイグレーションツールgh-ostで深夜メンテナンスを無くした話
s2terminal
0
30
Microsoft Azureで 女子力を生成する
s2terminal
0
47
かんたん機械学習はじめの1歩AzureMachineLearningでTweetをレコメンド
s2terminal
0
27
Other Decks in Technology
See All in Technology
エンジニアのためのドキュメント力基礎講座〜構造化思考から始めよう〜(2025/02/15jbug広島#15発表資料)
yasuoyasuo
17
6.8k
分解して理解する Aspire
nenonaninu
1
180
2/18/25: Java meets AI: Build LLM-Powered Apps with LangChain4j
edeandrea
PRO
0
120
データマネジメントのトレードオフに立ち向かう
ikkimiyazaki
6
990
一度 Expo の採用を断念したけど、 再度 Expo の導入を検討している話
ichiki1023
1
170
OpenID Connect for Identity Assurance の概要と翻訳版のご紹介 / 20250219-BizDay17-OIDC4IDA-Intro
oidfj
0
280
表現を育てる
kiyou77
1
210
2024.02.19 W&B AIエージェントLT会 / AIエージェントが業務を代行するための計画と実行 / Algomatic 宮脇
smiyawaki0820
13
3.5k
Helm , Kustomize に代わる !? 次世代 k8s パッケージマネージャー Glasskube 入門 / glasskube-entry
parupappa2929
0
250
Nekko Cloud、 これまでとこれから ~学生サークルが作る、 小さなクラウド
logica0419
2
970
TAMとre:Capセキュリティ編 〜拡張脅威検出デモを添えて〜
fujiihda
2
250
「海外登壇」という 選択肢を与えるために 〜Gophers EX
logica0419
0
710
Featured
See All Featured
Embracing the Ebb and Flow
colly
84
4.6k
Unsuck your backbone
ammeep
669
57k
Building a Scalable Design System with Sketch
lauravandoore
461
33k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
46
2.3k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
550
Automating Front-end Workflow
addyosmani
1368
200k
Testing 201, or: Great Expectations
jmmastey
42
7.2k
A designer walks into a library…
pauljervisheath
205
24k
Optimising Largest Contentful Paint
csswizardry
34
3.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
Transcript
20190610 kansai.ts #1 s2terminal / suzuki.sh Twitter@suzukiterminal GitHub@s2terminal Qiita@suzuki_sh Cognitive
Complexity で コードの複雑さを定量的に計測しよう
Self Introduction $ man s2terminal NAME: s2terminal or suzuki.sh JOB:
Web Application Engineer REGION: Nagoya (5 years), Osaka (2 years) SKILL: PHP, Ruby, Azure, AWS, and TypeScript LOVE: Etrian Odyssey, Ninja Slayer, Puella Magi Madoka Magica (Miki Sayaka), THE IDOLM@STER SHINY COLORS (Kuwayama Chiyuki)
を 使ってみた
Code Climate とは? • GitHub リポジトリと連携することで、 品質(Quality)や生産性(Velocity)を測定できるサービス • OSSなら、Quality機能は無料で利用可能 •
JavaScriptはもちろんTypeScriptにも標準対応
使ってみた結果 https://codeclimate.com/github/s2terminal/i-read-u/issues 拙作のOSS「I Read U」 TypeScript 製 CLIツール $ npm
install --global i-read-u 詳しくは: https://s2terminal.github.io/i-read-u/
使ってみた結果 “toCommandSections() メソッドが 29行もあるから、25行以下にしろよ” “1時間あれば直せるだろ” わかる お前誰?
使ってみた結果 “read()メソッドのCognitive Complexityが 8あるから、5以下にしろよ” “45分あれば直せるだろ” わからない お前誰?
Cognitive Complexity とは ? Photo by Shahadat Shemul on Unsplash
Cognitive Complexity is 何? Cognitive Complexity (コグニティブ・コンプレクシティー) 訳してみれば「認知的 複雑度」 コードの複雑さを測る指標のひとつ
2016年 SonarSource社が提唱
「Cyclomatic Complexity」と「Cognitive Complexity」 似たものに Cyclomatic Complexity(循環的複雑度) がある • 線形的に独立した経路の数 •
分岐網羅に必要なテストケースの数 約40年前に考案された Cyclomatic Complexity の 問題点を解決するための指標として、 あらたに Cognitive Complexity が考え出された、とのこと
Cyclomatic Complexity Cognitive Complexity サイクロマティック コンプレクシティー コグニティブ コンプレクシティー 循環的 複雑度
認知的 複雑度 1976 年 Thomas McCabe氏が考案 2016 年 SonarSource社が考案 機械的なテストの難しさを測る 人間にとっての理解の難しさを測る
どちらのコードが ”複雑” だと思いますか?
4 Cyclomatic Complexity 4 1 Cognitive Complexity 7
「Cyclomatic Complexity」と「Cognitive Complexity」 • Cyclomatic Complexity は、単純な switch 文で「4」 •
switch 文の分岐網羅のためにテストケースが 4 必要になるため • テストケースが増えるので複雑である、という考え方 • Cognitive Complexity は、単純な switch 文では「1」 • switch 文がひとつなので、+1 • 人間が読むには複雑ではない、という考え方 Cognitive Complexity を使うことで、ソースコードの 人間にとっての理解しやすさを定量的に計測できる
Cognitive Complexity の 具体例 Photo by Immo Wegmann on Unsplash
Code is considered more complex for each "break in the
linear flow of the code“ 「コードの線形的な流れを乱す」とき、より複雑とみなす
線形的な流れを乱すとき、複雑とみなす Cognitive Complexity: 0 Cognitive Complexity: 1 Cognitive Complexity: 1
線形的な流れを乱すとき、複雑とみなす +1 +1 +1 +1 Cognitive Complexity: 2 Cognitive Complexity:
2
Code is considered more complex when "flow breaking structures are
nested" 「線形的な流れを乱すコードが、ネストされている」ようなときは 一層複雑であるとみなす
流れを乱すネストが深いほど、複雑とみなす +1 +2 ( if 1 + nest 1 )
Cognitive Complexity: 3
流れを乱すネストが深いほど、複雑とみなす +1 +2 ( if 1 + nest 1 )
+3 ( if 1 + nest 2 ) Cognitive Complexity: 6
Code is not considered more complex when it uses shorthand
that the language provides for collapsing multiple statements into one 言語仕様によって提供されているショートハンドで 複数の式をひとつにまとめたようなときは、複雑であるとはみなされない
ショートハンドが使われていれば、複雑とみなさない Cognitive Complexity: 2 Cognitive Complexity: 1
冒頭の指摘の実例 (Cognitive Complexity 8) +1 +2 ( if 1 +
nest 1) +1 +1 +3 ( if 1 + nest 2 )
Cognitive Complexity の 使い方 Photo by: https://pixabay.com/images/id-1463929/
Cognitive Complexity を測るには? • Code Climate を使うことで、Cognitive Complexity など さまざまな指標を測定できる
• OSSなら、Quality機能は無料で利用可能
Cognitive Complexity の計測を手軽に試せる環境を作った https://github.com/s2terminal/cognitive-complexity-example git clone して、index.ts 等を適当に編集したのち docker-compose run
--rm analyze コマンドを打つと Cognitive Complexity を算出してくれる (※Windows非対応) やっていることは、CodeClimate CLI 版の Docker イメージ持ってきて Cognitive Complexity だけ 許容値-1(ゼロ以上でアラート)で動かしているだけ。
まとめ • Cognitive Complexity の低い、読みやすいコードを書こう • 分岐を減らす • ネストを減らす •
ショートハンドを使う • Code Climate で Cognitive Complexity を測定できる • OSSなら無料 • TypeScript にも標準対応 • CLI版もあり、Dockerで動作する
おまけ: NGな使い方 • 「お前のコード Cognitive Complexity 高すぎない?」と 新人にコードレビューしてはいけません • 新人が死にます
• 「俺なら45分で直せる」と 新人にコードレビューしてはいけません • 新人が死にます
参考 • Cognitive Complexity - Code Climate • https://docs.codeclimate.com/docs/cognitive-complexity •
COGNITIVE COMPLEXITY - A new way of measuring understandability - G. Ann Campbell • https://www.sonarsource.com/docs/CognitiveComplexity.pdf • Cognitive Complexity で、コードの読みやすさを定量的に計測しよう • https://qiita.com/suzuki_sh/items/824c36b8d53dd2f1efcb • 新人プログラマをレビューで殺さない方法 • https://qiita.com/hiraike32/items/32840b11536fa1b78621 • Code Climate • https://codeclimate.com/ • 画像の出典 • https://unsplash.com/photos/qnrJoo2_4EQ • https://unsplash.com/photos/O2MdroNurVw • https://pixabay.com/photos/pasta-spaghetti-food-italian-1463929/#content
Thank you! follow me https://twitter.com/suzukiterminal