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>の計測ツールをgo/a...
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
株式会社ビットキー / Bitkey Inc.
PRO
April 23, 2022
890
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
人間の直感に対応させた複雑度<Cognitive Complexity>の計測ツールをgo/astで実装してみた
株式会社ビットキー / Bitkey Inc.
PRO
April 23, 2022
More Decks by 株式会社ビットキー / Bitkey Inc.
See All by 株式会社ビットキー / Bitkey Inc.
失敗を経て、Harness Engineering で 大切にしたいことを考える / Learning from Failure: What Matters in Harness Engineering
bitkey
PRO
1
430
AI Adaptable なテストを整える工夫 / Ways to Make Your Tests AI-Adaptable
bitkey
PRO
3
260
人が担う「価値」とは?これからの「QA」とは / Human Value and the Future of Quality Assurance
bitkey
PRO
0
250
キャリア25年目にしてTypeScript に出会うまで - 「型」を通じて振り返るプログラミング言語遍歴 / Meeting TypeScript After 25 Years in Tech - Looking Back at My Programming Language Journey Through "Types"
bitkey
PRO
2
440
AIのために、AIを使った、Effect-TSからの脱却 〜テストを活用した安全なリファクタリングの進め方〜
bitkey
PRO
2
890
Flutter初心者が生成AIで大規模アプリ開発をキャッチアップした工夫 〜元ネイティブエンジニアが実践した、技術転換の高速道路〜 / Flutter with LLM: A Former Native Engineer's Fast Track to Large-Scale Apps
bitkey
PRO
0
310
〜備えあれば憂いなし〜とりあえず障害訓練やろ? デジタル/フィジカル横断プロダクトを24365で維持するための戦略 / Better Safe Than Sorry: Incident Drills for 24/365 Digital-Physical Products
bitkey
PRO
0
170
雑談は、センサーだった
bitkey
PRO
2
550
AIが書いたコードを信じられない問題 〜レビュー負荷を下げるために変えたこと〜 / The AI Code Trust Gap: Reducing the Review Burden
bitkey
PRO
8
1.6k
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Navigating Weather and Climate Data
rabernat
0
280
A Tale of Four Properties
chriscoyier
163
24k
How to train your dragon (web standard)
notwaldorf
97
6.7k
GraphQLとの向き合い方2022年版
quramy
50
15k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
370
Utilizing Notion as your number one productivity tool
mfonobong
4
350
30 Presentation Tips
portentint
PRO
1
340
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
380
Transcript
Bitkey Inc. Connect Product Noriki Takahashi ਓؒͷײʹରԠͤͨ͞ෳࡶ<Cognitive Complexity>ͷܭଌπʔϧΛgo/ astͰ࣮ͯ͠Έͨ
Outline 1. ࣗݾհ 2. ιʔείʔυͷෳࡶ 3. Cognitive Complexityͱʁ 4. go/ast
ͱʁ 5. ෳࡶܭଌπʔϧ࣮ 6. ֶͼͱิ 7. ·ͱΊ 2 2
1. ࣗݾհ 3 • ∁ڮయੜ(Noriki Takahashi) ◦ Twitter: @norinoriki0 ◦
GitHub: nrnrk • גࣜձࣾBitkey ◦ bitkey platform ͱ͍͏”ΧΪ”ͷϓϥοτϑΥʔϜͷ։ൃ • Go Ͱ։ൃ͍ͯ͠·͕͢ɺ go/ast ͜Ε·Ͱ৮ͬͨ͜ͱ͕͋Γ·ͤΜͰͨ͠ • झຯ: ֶɾཧֶ ◦ zk-SNARKs ͷֶతͳଆ໘ΛטΈࡅ͘هࣄΛॻ͍ͨΓͯ͠·͢ • #gocon ͰײͳͲੋඇΑΖ͓͘͠Ͷ͕͍͠·͢ʂ
• Bitkey ͷ3ͭͷࣄۀυϝΠϯ ◦ HomeʢΒ͠ʣ ◦ Workspaceʢಇ͘ʣ ◦ Experienceʢඇৗͷମݧʣ •
bitkey platform ◦ 3ͭͷ hub ΞϓϦέʔγϣϯͷج൫ 1. ࣗݾհ 4 ςΫϊϩδʔͷྗͰ͋ΒΏΔͷΛ ҆શͰ ศརͰ ؾ࣋ͪΑ͘ʮͭͳ͛Δʯ ͓͢͢Ί͍ͨ͠GoϥΠϒϥϦථडத!
• Bitkey ͷ3ͭͷࣄۀυϝΠϯ ◦ HomeʢΒ͠ʣ ◦ Workspaceʢಇ͘ʣ ◦ Experienceʢඇৗͷମݧʣ •
bitkey platform ◦ 3ͭͷ hub ΞϓϦέʔγϣϯͷج ൫ 1. ࣗݾհ 5 ςΫϊϩδʔͷྗͰ͋ΒΏΔͷΛ ҆શͰ ศརͰ ؾ࣋ͪΑ͘ʮͭͳ͛Δʯ ͓͢͢Ί͍ͨ͠GoϥΠϒϥϦථडத! എܠͷ bitkey x Gopher͘ΜͷίϥϘʹʂ
ιʔείʔυͷෳࡶ 6 6
ಥવͰ͕͢ Έͳ͞Μ 7 7 2. ιʔείʔυͷෳࡶ
ෳࡶͳιʔείʔυ͖Ͱ͔͢ʁ 8 8 2. ιʔείʔυͷෳࡶ
ෳࡶͳιʔείʔυ͖Ͱ͔͢ʁ 9 9 2. ιʔείʔυͷෳࡶ
ࢲͦΜͳʹ͖͡Ό͋Γ·ͤΜ 10 10 2. ιʔείʔυͷෳࡶ
ࢲͦΜͳʹ͖͡Ό͋Γ·ͤΜ 11 11 2. ιʔείʔυͷෳࡶ
Ͱ͖Δ͚ͩγϯϓϧʹͯ͠ อक͍ͨ͘͢͠͠ 12 12 2. ιʔείʔυͷෳࡶ
ෳࡶ͕͞ආ͚ΒΕͳ͍ͱ͖ ςετΛॏతʹߦ͍͍ͨ 13 13 2. ιʔείʔυͷෳࡶ
͜ΕΒΛܧଓతʹ࣮ݱ͍ͯͨ͘͠Ίʹ 14 14 2. ιʔείʔυͷෳࡶ
ෳࡶΛఆྔతʹද͍ͨ͠ 15 15 2. ιʔείʔυͷෳࡶ
ௐͨͱ͜Ζ 16 16 2. ιʔείʔυͷෳࡶ
ௐͨͱ͜Ζ 17 17 2. ιʔείʔυͷෳࡶ
ਓؒͷײ͡Δෳࡶ͞ʹରԠͨ͠ ࢦඪ͕͋ΔΒ͍͠ 18 18 2. ιʔείʔυͷෳࡶ
ͦΕ͕ 19 19 2. ιʔείʔυͷෳࡶ
Cognitive Complexity 20 20 2. ιʔείʔυͷෳࡶ
Cognitive Complexity 21 21 2. ιʔείʔυͷෳࡶ
Cognitive Complexity ͱ 22 22
• Complexity (ෳࡶ) ͱ 3. Cognitive Complexityͱʁ 23
• Complexity (ෳࡶ) ͱ ◦ ֊͕ਂ͍ͱෳࡶʁ 24 3. Cognitive Complexityͱʁ
• Complexity (ෳࡶ) ͱ ◦ ֊͕ਂ͍ͱෳࡶʁ ◦ ذ͕ଟ͍ͱෳࡶʁ 25 3.
Cognitive Complexityͱʁ
• Complexity (ෳࡶ) ͱ ◦ ֊͕ਂ͍ͱෳࡶʁ ◦ ذ͕ଟ͍ͱෳࡶʁ ◦ ίʔυྔ͕ଟ͍ͱෳࡶʁ
26 3. Cognitive Complexityͱʁ
• Complexity (ෳࡶ) ͱ ◦ ֊͕ਂ͍ͱෳࡶʁ ◦ ذ͕ଟ͍ͱෳࡶʁ ◦ ίʔυྔ͕ଟ͍ͱෳࡶʁ
◦ ߦ͕ଟ͍ͱෳࡶʁ 27 3. Cognitive Complexityͱʁ
• Complexity (ෳࡶ) ͱ ◦ ֊͕ਂ͍ͱෳࡶʁ ◦ ذ͕ଟ͍ͱෳࡶʁ ◦ ίʔυྔ͕ଟ͍ͱෳࡶʁ
◦ ߦ͕ଟ͍ͱෳࡶʁ → ͢ΔϙΠϯτʹΑͬͯࢦඪ͕ҟͳΔ 28 3. Cognitive Complexityͱʁ
• ༷ʑͳෳࡶͷࢦඪ ◦ ॥తෳࡶ (Cyclomatic Complexity) ▪ ઢܗతʹಠཱͨ͠ܦ࿏ͷʹͨ͠ࢦඪ ▪ (ͬ͘͟Γ͍͏ͱ)
ܦ࿏ͷύλʔϯ͕ଟ͍ͱෳࡶͱఆ ◦ Halstead ෳࡶ ▪ ͞“ޠኮ”ͷʹͨ͠ࢦඪ ▪ (ͬ͘͟Γ͍͏ͱ) ͍ͷදݱύλʔϯ͕ଟ͍ͱෳࡶͱఆ 29 3. Cognitive Complexityͱʁ
• ༷ʑͳෳࡶͷࢦඪ ◦ ॥తෳࡶ (Cyclomatic Complexity) ▪ ઢܗతʹಠཱͨ͠ܦ࿏ͷʹͨ͠ࢦඪ ▪ (ͬ͘͟Γ͍͏ͱ)
ܦ࿏ͷύλʔϯ͕ଟ͍ͱෳࡶͱఆ ◦ Halstead ෳࡶ ▪ ͞“ޠኮ”ͷʹͨ͠ࢦඪ ▪ (ͬ͘͟Γ͍͏ͱ) ͍ͷදݱύλʔϯ͕ଟ͍ͱෳࡶͱఆ 30 3. Cognitive Complexityͱʁ ༗໊ʂֶతͳϞσϧʹجͮ͘ʂ
• ೝతෳࡶ (Cognitive Complexity) ◦ ਓؒͷײͱ͍ۙෳࡶ͞Λࢦͨ͠ࢦඪ ◦ ίʔυͷઢܗͷྲྀΕΛཚ͢ͷʹ ◦ 2016ʹੜ
▪ Cyclomatic Complexity: 1976 31 White Paper: https://sonarsource.com/docs/CognitiveComplexity.pdf 3. Cognitive Complexityͱʁ
• ೝతෳࡶ (Cognitive Complexity) ◦ ਓؒͷײͱ͍ۙෳࡶ͞Λࢦͨ͠ࢦඪ ◦ ίʔυͷઢܗͷྲྀΕΛཚ͢ͷʹ ◦ 2016ʹੜ
▪ Cyclomatic Complexity: 1976 32 White Paper: https://sonarsource.com/docs/CognitiveComplexity.pdf 3. Cognitive Complexityͱʁ
“ਓؒͷײͱ͍ۙ” ͱʁ 33 33 3. Cognitive Complexityͱʁ
࣮ྫͰ֬ೝͯ͠ΈΔ 34 34 3. Cognitive Complexityͱʁ
͜͜Ͱ࣭Ͱ͢ 35 35 3. Cognitive Complexityͱʁ
࣍ͷ̎ͭͲͪΒ͕ෳࡶʁ 36 36 3. Cognitive Complexityͱʁ
37 37 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖ͨ͠ͷ
Ͳ͕ͬͪෳࡶʁ 3. Cognitive Complexityͱʁ
Cyclomatic Complexity ͷ߹ 38 38 3. Cognitive Complexityͱʁ
39 39 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖ͨ͠ͷ
3. Cognitive Complexityͱʁ
40 40 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖ͨ͠ͷ
ಉ͡ʂ 3. Cognitive Complexityͱʁ
Cognitive Complexity ͷ߹ 41 41 3. Cognitive Complexityͱʁ
42 42 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖ͨ͠ͷ
3. Cognitive Complexityͱʁ
43 43 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖ͨ͠ͷ
େࠩʂ 3. Cognitive Complexityͱʁ
Cognitive Complexity ͍͍Ͷʂ 44 44 3. Cognitive Complexityͱʁ
45 ࢀߟ: White Pager (Appendix B) https://sonarsource.com/docs/CognitiveComplexity.pdf • ෳࡶܭࢉϧʔϧ 3.
Cognitive Complexityͱʁ
46 ࢀߟ: White Pager (Appendix B) https://sonarsource.com/docs/CognitiveComplexity.pdf • ෳࡶܭࢉϧʔϧ 1.
ҎԼͷཁૉ͕͋Ε +1 for, switch, goto, if, else, if else, goto/break/continue <label> 3. Cognitive Complexityͱʁ
47 ࢀߟ: White Pager (Appendix B) https://sonarsource.com/docs/CognitiveComplexity.pdf • ෳࡶܭࢉϧʔϧ 1.
ҎԼͷཁૉ͕͋Ε +1 for, switch, goto, if, else, if else, goto/break/continue <label> 1. ωετ࣌ɺ1. ʹՃͰ + (ωετͷਂ͞) 3. Cognitive Complexityͱʁ
48 ࢀߟ: White Pager (Appendix B) https://sonarsource.com/docs/CognitiveComplexity.pdf • ෳࡶܭࢉϧʔϧ 1.
ҎԼͷཁૉ͕͋Ε +1 for, switch, goto, if, else, if else, goto/break/continue <label> 1. ωετ࣌ɺ1. ʹՃͰ + (ωετͷਂ͞) 3. Cognitive Complexityͱʁ
• ෳࡶܭࢉϧʔϧ 1. ҎԼͷཁૉ͕͋Ε +1 for, switch, goto, if, else,
if else, goto/break/continue <label> 1. ωετ࣌ɺ1. ʹՃͰ + (ωετͷਂ͞) 49 ࢀߟ: White Pager (Appendix B) https://sonarsource.com/docs/CognitiveComplexity.pdf for if Ͱෳࡶ૿Ճ 3. Cognitive Complexityͱʁ
• ෳࡶܭࢉϧʔϧ 1. ҎԼͷཁૉ͕͋Ε +1 for, switch, goto, if, else,
if else, goto/break/continue <label> 1. ωετ࣌ɺ1. ʹՃͰ + (ωετͷਂ͞) 50 ࢀߟ: White Pager (Appendix B) https://sonarsource.com/docs/CognitiveComplexity.pdf 3. Cognitive Complexityͱʁ ωετͷਂ͕͞Ճࢉʹ
• ෳࡶܭࢉϧʔϧ 1. ҎԼͷཁૉ͕͋Ε +1 for, switch, goto, if, else,
if else, goto/break/continue <label> 1. ωετ࣌ɺ1. ʹՃͰ + (ωετͷਂ͞) 51 ࢀߟ: White Pager (Appendix B) https://sonarsource.com/docs/CognitiveComplexity.pdf 3. Cognitive Complexityͱʁ early return OK continue <label> NG
Go ͷϓϩάϥϜΛܭଌͯ͠Έ͍ͨʂ 52 52 3. Cognitive Complexityͱʁ
੩తղੳπʔϧΛ࡞ΔͨΊͷ ϥΠϒϥϦͨ͘͞Μ͋Δ 53 53 3. Cognitive Complexityͱʁ
golang.org/x/tools/go/analysis golang.org/x/tools/go/ast/inspector golang.org/x/tools/go/ast/astuil golang.org/x/tools/go/ssa … 54 54 3. Cognitive Complexityͱʁ
੩తղੳॳ৺ऀ͔Β͢Δͱ.. 55 55 3. Cognitive Complexityͱʁ
֓೦͕ଟͯ͘ཧղ͕େม͔... 56 56 3. Cognitive Complexityͱʁ
57 57 3. Cognitive Complexityͱʁ ֓೦͕ଟͯ͘ཧղ͕େม͔...
େݩʹ͋Δඪ४ͷ go/ast ͚ͩͰे࡞ΕΔ 58 58 3. Cognitive Complexityͱʁ
੩తղੳͷ࠷ॳͷҰาͱͯ͠ جૅͱͯ͠ go/ast Λཧղ͍ͨ͠...! 59 59 3. Cognitive Complexityͱʁ
Cognitive Complexity ܭଌπʔϧ go/ast Ͱ࣮ͯ͠جຊΛཧղ͠Α͏ 60 60 3. Cognitive Complexityͱʁ
Cognitive Complexity ܭଌπʔϧ go/ast Ͱ࣮ͯ͠جຊΛཧղ͠Α͏ 61 61 3. Cognitive Complexityͱʁ
go/ast ͱʁ 62 62
• go/ast ͱ ◦ ߏจΛදݱ͢ΔͨΊʹ༻͞ΕΔܕ͕ೖͬͨύοέʔδ ◦ linter Λ࡞Δͱ͖ͳͲʹར༻͞Ε͍ͯΔ 4. go/ast
ͱʁ 63
• go/ast ͱ ◦ ߏจΛදݱ͢ΔͨΊʹ༻͞ΕΔܕ͕ೖͬͨύοέʔδ ◦ linter Λ࡞Δͱ͖ͳͲʹར༻͞Ε͍ͯΔ • AST(நߏจ)ͱ
◦ ιʔείʔυΛѻ͍͍͢Α͏ʹߏʹͨ͠ͷ ◦ Abstract Syntax Tree ͷུ 4. go/ast ͱʁ 64
(ந)ߏจͬͯ݁ہͳΜͩ? 65 65 4. go/ast ͱʁ
ιʔείʔυͷίϯύΠϧ࣌ͷ ྲྀΕͰ֬ೝͯ͠ΈΔ 66 66 4. go/ast ͱʁ
67 67 ιʔείʔυ ࣮ߦϑΝΠϧ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱʁ ίϯύΠϧ
68 68 ιʔείʔυ ߏจ ࣮ߦϑΝΠϧ ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ &
࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱʁ
69 69 ιʔείʔυ ߏจ ࣮ߦϑΝΠϧ func main() { result :=
`apple` + `pen` … } ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ & ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱʁ
70 70 ιʔείʔυ ߏจ ࣮ߦϑΝΠϧ func main() { result :=
`apple` + `pen` … } ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ & ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱʁ apple pen +
71 71 ιʔείʔυ ߏจ ࣮ߦϑΝΠϧ func main() { result :=
`apple` + `pen` … } ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ & ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱʁ apple pen +
72 72 ιʔείʔυ ߏจ ࣮ߦϑΝΠϧ func main() { result :=
`apple` + `pen` … } ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ & ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱʁ apple pen +
73 73 ιʔείʔυ ߏจ ࣮ߦϑΝΠϧ func main() { result :=
`apple` + `pen` … } ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ & ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ ߏԽ͞Ε͍ͯΔ͓͔͛Ͱ... ίʔυΛνΣοΫ͢Δॲཧ͕ॻ͖͍͢ (ྫ: ه๏ͷνΣοΫɺύοέʔδґଘͳͲ) AST = go/ast Ͱ͜ͷߏΛར༻Ͱ͖ Δ 4. go/ast ͱʁ apple pen +
: ࣮Go ͷίϯύΠϥͰ go/ast Λ͍ͬͯͳ͍ → go/ast ੩తղੳπʔϧͳͲͰར༻͞Ε͍ͯΔ 74 74
4. go/ast ͱʁ
• go/ast Ͱදݱ͞ΕΔߏ 75 4. go/ast ͱʁ
• go/ast Ͱදݱ͞ΕΔߏ 76 Node Node Node Node Node Node
4. go/ast ͱʁ
• go/ast Ͱදݱ͞ΕΔߏ 77 Node Node Node Node Node Node
4. go/ast ͱʁ ۩ମྫΛ go/ast ͷυΩϡϝϯτͰνΣοΫʂ
AssignStmt BinaryExpr BlockStmt BranchStmt CallExpr DeclStmt DeferStmt EmptyStmt ExprStmt ForStmt
78 78 4. go/ast ͱʁ FuncDecl GenDecl GoStmt IfStmt ImportSpec IncDecStmt IndexExpr KeyValueExpr LabeledStmt ParenExpr RangeStmt ReturnStmt SelectStmt SelectorExpr SendStmt SliceExpr StartExpr SwitchStmt TypeAssertExpr TypeSpec TypeSwitchStmt UnaryExpr ValueSpec ͳͲ
AssignStmt BinaryExpr BlockStmt BranchStmt CallExpr DeclStmt DeferStmt EmptyStmt ExprStmt ForStmt
79 79 4. go/ast ͱʁ FuncDecl GenDecl GoStmt IfStmt ImportSpec IncDecStmt IndexExpr KeyValueExpr LabeledStmt ParenExpr RangeStmt ReturnStmt SelectStmt SelectorExpr SendStmt SliceExpr StartExpr SwitchStmt TypeAssertExpr TypeSpec TypeSwitchStmt UnaryExpr ValueSpec ͳͲ େྔ...! Θ͚͕Θ͔Βͳ͍...
AssignStmt BinaryExpr BlockStmt BranchStmt CallExpr DeclStmt DeferStmt EmptyStmt ExprStmt ForStmt
80 80 4. go/ast ͱʁ FuncDecl GenDecl GoStmt IfStmt ImportSpec IncDecStmt IndexExpr KeyValueExpr LabeledStmt ParenExpr RangeStmt ReturnStmt SelectStmt SelectorExpr SendStmt SliceExpr StartExpr SwitchStmt TypeAssertExpr TypeSpec TypeSwitchStmt UnaryExpr ValueSpec ͳͲ ΜʁΑ͘ݟΔͱඌ͕ڞ௨͍ͯ͠Δ͕ͭ͋Δͳ
AssignStmt BinaryExpr BlockStmt BranchStmt CallExpr DeclStmt DeferStmt EmptyStmt ExprStmt ForStmt
81 81 4. go/ast ͱʁ FuncDecl GenDecl GoStmt IfStmt ImportSpec IncDecStmt IndexExpr KeyValueExpr LabeledStmt ParenExpr RangeStmt ReturnStmt SelectStmt SelectorExpr SendStmt SliceExpr StartExpr SwitchStmt TypeAssertExpr TypeSpec TypeSwitchStmt UnaryExpr ValueSpec ͳͲ
AssignStmt BinaryExpr BlockStmt BranchStmt CallExpr DeclStmt DeferStmt EmptyStmt ExprStmt ForStmt
82 82 4. go/ast ͱʁ FuncDecl GenDecl GoStmt IfStmt ImportSpec IncDecStmt IndexExpr KeyValueExpr LabeledStmt ParenExpr RangeStmt ReturnStmt SelectStmt SelectorExpr SendStmt SliceExpr StartExpr SwitchStmt TypeAssertExpr TypeSpec TypeSwitchStmt UnaryExpr ValueSpec ͳͲ υΩϡϝϯτABCॱʹฒΜͰ͍Δ͕ ߏମ໊ͷඌʹͯ͠ྨ͢ΔͱΘ͔Γ͍͢
Node ͷྨ ɾ Stmt : จ (Statement) ɾ Expr :
ࣜ(Expression) ɾ Decl : એݴ(Declaration) ɾ Spec : εϖοΫ(Specification) ɾ Type : ܕ ɾ Lit : Ϧςϥϧ(Literal) etc… • go/ast Ͱදݱ͞ΕΔߏ 83 Node Node Node Node Node Node 4. go/ast ͱʁ
Node ͷྨ ɾ Stmt : จ (Statement) ɾ Expr :
ࣜ(Expression) ɾ Decl : એݴ(Declaration) ɾ Spec : εϖοΫ(Specification) ɾ Type : ܕ ɾ Lit : Ϧςϥϧ(Literal) etc… • go/ast Ͱදݱ͞ΕΔߏ 84 Node Node Node Node Node Node 4. go/ast ͱʁ ιʔείʔυ্࣮ interface ͕ ͪΌΜͱఆٛ͞Ε͍ͯΔ
࣮ྫͰνΣοΫʂ 85 85 4. go/ast ͱʁ
4. go/ast ͱʁ gopher.go
4. go/ast ͱʁ gopher.go main.go : “gopher.go” ͷ AST Λग़ྗ͢Δ
4. go/ast ͱʁ gopher.go main.go : “gopher.go” ͷ AST Λग़ྗ͢Δ
AST ʹม AST Λग़ྗ
File GenDecl FuncDecl Ident BlockStm t CallExpr ImportSpec SelectorExpr BasicLit
Ident FuncType FieldList BasicLit Ident Ident 4. go/ast ͱʁ gopher.go
File GenDecl FuncDecl Ident BlockStm t CallExpr ImportSpec SelectorExpr BasicLit
Ident FuncType FieldList BasicLit Ident Ident main import (શମ) import (1ͭ) “fmt” () (Ҿͳ͠) “gopher ” func {} fmt Printl n “Gopher! !” ( ) . 4. go/ast ͱʁ gopher.go
File GenDecl FuncDecl Ident BlockStm t CallExpr ImportSpec SelectorExpr BasicLit
Ident FuncType FieldList BasicLit Ident Ident main import (શମ) import (1ͭ) “fmt” () (Ҿͳ͠) “gopher ” func {} fmt Printl n “Gopher! !” ( ) File ͕ϧʔτʂ . 4. go/ast ͱʁ gopher.go
File GenDecl FuncDecl Ident BlockStm t CallExpr ImportSpec SelectorExpr BasicLit
Ident FuncType FieldList BasicLit Ident Ident main import (શମ) import (1ͭ) “fmt” () (Ҿͳ͠) “gopher ” func {} . fmt Printl n “Gopher! !” ( ) File ͕ϧʔτʂ 4. go/ast ͱʁ gopher.go
File GenDecl FuncDecl Ident BlockStm t CallExpr ImportSpec SelectorExpr BasicLit
Ident FuncType FieldList BasicLit Ident Ident main import (શମ) import (1ͭ) “fmt” () (Ҿͳ͠) “gopher ” func {} . fmt Printl n “Gopher! !” ( ) File ͕ϧʔτʂ 4. go/ast ͱʁ gopher.go
File GenDecl FuncDecl Ident BlockStm t CallExpr ImportSpec SelectorExpr BasicLit
Ident FuncType FieldList BasicLit Ident Ident main import (શମ) import (1ͭ) “fmt” () (Ҿͳ͠) “gopher ” func {} . fmt Printl n “Gopher! !” ( ) File ͕ϧʔτʂ 4. go/ast ͱʁ gopher.go
File GenDecl FuncDecl Ident BlockStm t CallExpr ImportSpec SelectorExpr BasicLit
Ident FuncType FieldList BasicLit Ident Ident main import (શମ) import (1ͭ) “fmt” () (Ҿͳ͠) “gopher ” func {} . fmt Printl n “Gopher! !” ( ) File ͕ϧʔτʂ 4. go/ast ͱʁ gopher.go
ෳࡶܭଌπʔϧ࣮ 96 96
• ෳࡶܭଌπʔϧ ◦ ιʔείʔυ͔Β֤ؔ͝ͱͷ Cognitive Complexity Λܭࢉ͢Δ 5. ෳࡶܭଌπʔϧ࣮ 97
• ෳࡶܭଌπʔϧ ◦ ιʔείʔυ͔Β֤ؔ͝ͱͷ Cognitive Complexity Λܭࢉ͢Δ • શମͷߏ 1.
ιʔείʔυ͔ΒASTΛੜ 2. AST͔ΒؔͷϊʔυͷΈநग़ 3. ֤ؔͷෳࡶΛܭࢉ 98 5. ෳࡶܭଌπʔϧ࣮
ᶃ ιʔείʔυ͔ΒASTΛੜ 99 99 5. ෳࡶܭଌπʔϧ࣮
ᶃ ιʔείʔυ͔ΒASTΛੜ 100 100 5. ෳࡶܭଌπʔϧ࣮
ᶃ ιʔείʔυ͔ΒASTΛੜ 101 101 ϧʔτʹͳΔ Node (File) ͕औಘͰ͖Δ ଞͷNode͔͜͜ΒͨͲΔ͜ͱ͕Ͱ͖Δ 5.
ෳࡶܭଌπʔϧ࣮
ᶄ AST͔ΒؔͷϊʔυͷΈநग़ 102 102 5. ෳࡶܭଌπʔϧ࣮
ᶄ AST͔ΒؔͷϊʔυͷΈநग़ 103 103 ᶃ ͷ File (Node) 5. ෳࡶܭଌπʔϧ࣮
ᶄ AST͔ΒؔͷϊʔυͷΈநग़ 104 104 ίʔυͷதͷؔͷΈΛऔΓग़͢ → ᶅ Ͱؔຖʹॲཧ ᶃ ͷ
File (Node) 5. ෳࡶܭଌπʔϧ࣮
ᶅ ֤ؔͷෳࡶΛܭࢉ 105 105 5. ෳࡶܭଌπʔϧ࣮
3-1. ෳࡶͷܭࢉϧʔϧ࠶֬ೝ 106 106 5. ෳࡶܭଌπʔϧ࣮
• ෳࡶܭࢉϧʔϧ(࠶ܝ) 1. ҎԼͷཁૉ͕͋Ε +1 for, switch, goto, if, else,
if else, goto/break/continue <label> 1. ωετ࣌ɺ1. ʹՃͰ + (ωετͷਂ͞) 107 ࢀߟ: White Pager (Appendix B) https://sonarsource.com/docs/CognitiveComplexity.pdf 5. ෳࡶܭଌπʔϧ࣮
3-2. ॲཧͷશମ૾ 108 108 5. ෳࡶܭଌπʔϧ࣮
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ Visitor Ͱ֤ϊʔυΛࠪͤ͞Δ 109 109 FuncDecl CallExpr ForStmt
ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST 5. ෳࡶܭଌπʔϧ࣮
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ Visitor Ͱ֤ϊʔυΛࠪͤ͞Δ 110 110 FuncDecl CallExpr ForStmt
ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> 5. ෳࡶܭଌπʔϧ࣮
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ Visitor Ͱ֤ϊʔυΛࠪͤ͞Δ 111 111 FuncDecl CallExpr ForStmt
ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) 5. ෳࡶܭଌπʔϧ࣮
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ Visitor Ͱ֤ϊʔυΛࠪͤ͞Δ 112 112 FuncDecl CallExpr ForStmt
ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) 5. ෳࡶܭଌπʔϧ࣮
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ Visitor Ͱ֤ϊʔυΛࠪͤ͞Δ 113 113 FuncDecl CallExpr ForStmt
ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) (+1) 5. ෳࡶܭଌπʔϧ࣮
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ Visitor Ͱ֤ϊʔυΛࠪͤ͞Δ 114 114 FuncDecl CallExpr ForStmt
ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) (+1) ෳࡶ: 4 5. ෳࡶܭଌπʔϧ࣮
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ Visitor Ͱ֤ϊʔυΛࠪͤ͞Δ 115 115 FuncDecl CallExpr ForStmt
ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) (+1) ෳࡶ: 4 5. ෳࡶܭଌπʔϧ࣮ ࣮ͷࢀߟ: https://github.com/fzipp/gocyclo, https://github.com/uudashr/gocognit
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ Visitor Ͱ֤ϊʔυΛࠪͤ͞Δ 116 116 FuncDecl CallExpr ForStmt
ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) (+1) ෳࡶ: 4 ෳࡶΛอ࣋ 5. ෳࡶܭଌπʔϧ࣮ ࣮ͷࢀߟ: https://github.com/fzipp/gocyclo, https://github.com/uudashr/gocognit
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ Visitor Ͱ֤ϊʔυΛࠪͤ͞Δ 117 117 FuncDecl CallExpr ForStmt
ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) (+1) ෳࡶ: 4 ωετͷਂ͞Λอ࣋ 5. ෳࡶܭଌπʔϧ࣮ ࣮ͷࢀߟ: https://github.com/fzipp/gocyclo, https://github.com/uudashr/gocognit
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ Visitor Ͱ֤ϊʔυΛࠪͤ͞Δ 118 118 FuncDecl CallExpr ForStmt
ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) (+1) ෳࡶ: 4 ࣮ͷࢀߟ: https://github.com/fzipp/gocyclo, https://github.com/uudashr/gocognit ܭࢉϧʔϧͷΤοδέʔεͷରԠ (ࣗݾ࠶ؼͷݕ ʮelse if ͱ ifʯͷ۠ผ ) ※ ݸผͷͳͷͰؾʹ͠ͳͯ͘OK 5. ෳࡶܭଌπʔϧ࣮
֤ϊʔυͷࠪॲཧͬͯͲ͏͢Ε 119 119 5. ෳࡶܭଌπʔϧ࣮
go/ast ʹ͕ؔ༻ҙ͞Ε͍ͯΔ 120 120 5. ෳࡶܭଌπʔϧ࣮
3-3. ࠪͷ࣮ 121 121 5. ෳࡶܭଌπʔϧ࣮
• ࠪͷ࣮ 122 122 ast.Walk ࢦఆͨ͠ node ҎԼΛ visitor Ͱࠪ
ast.Visitor ࢦఆ͞Εͨ node ΛVisitͰࠪ 5. ෳࡶܭଌπʔϧ࣮
• ࠪͷ࣮ 123 123 ast.Walk ࢦఆͨ͠ node ҎԼΛ visitor Ͱࠪ
ast.Visitor ࢦఆ͞Εͨ node ΛVisitͰࠪ Visit ͷ࣮ྫ 5. ෳࡶܭଌπʔϧ࣮
• ࠪͷ࣮ 124 124 ast.Walk ࢦఆͨ͠ node ҎԼΛ visitor Ͱࠪ
ast.Visitor ࢦఆ͞Εͨ node ΛVisitͰࠪ }Node͝ͱʹ ذͯ͠ॲཧ Visit ͷ࣮ྫ 5. ෳࡶܭଌπʔϧ࣮
• ࠪͷ࣮ 125 125 ast.Walk ࢦఆͨ͠ node ҎԼΛ visitor Ͱࠪ
ast.Visitor ࢦఆ͞Εͨ node ΛVisitͰࠪ }Node͝ͱʹ ذͯ͠ॲཧ Visit ͷ࣮ྫ ͜͜ͷதͲΜͳײ͡ʁ 5. ෳࡶܭଌπʔϧ࣮
• ࠪͷ࣮ ྫ: ForStmt ༻ͷॲཧ 126 126 5. ෳࡶܭଌπʔϧ࣮
1 + (ωετͷਂ͞) ͚ͩෳࡶΛՃ • ࠪͷ࣮ ྫ: ForStmt ༻ͷॲཧ 127
127 5. ෳࡶܭଌπʔϧ࣮
• ࠪͷ࣮ ྫ: ForStmt ༻ͷॲཧ ॳظԽɾ݅ɾมԽʹཁૉ͕ ͋ΕͦͪΒΛࠪ (ਂ͞༏ઌ) 128 128
5. ෳࡶܭଌπʔϧ࣮
• ࠪͷ࣮ ྫ: ForStmt ༻ͷॲཧ ωετΛ1ͭਂͯ͘͠ຊମॲཧ ↓ ऴΘͬͨΒωετΛ1ͭ͢ 129 129
5. ෳࡶܭଌπʔϧ࣮
ᶅ ֤ؔͷෳࡶΛܭࢉ ྃʂ 130 130 5. ෳࡶܭଌπʔϧ࣮
ᶃʙᶅΛΈ߹ΘͤΔ 131 131 5. ෳࡶܭଌπʔϧ࣮
132 132 5. ෳࡶܭଌπʔϧ࣮
133 133 5. ෳࡶܭଌπʔϧ࣮
134 134 5. ෳࡶܭଌπʔϧ࣮
135 135 5. ෳࡶܭଌπʔϧ࣮
ͪ͜Β https://github.com/nrnrk/gocognito 136 136 5. ෳࡶܭଌπʔϧ࣮
ײ 137 137 5. ෳࡶܭଌπʔϧ࣮
go/ast ศརʂָ͍͠ʂ 138 138 5. ෳࡶܭଌπʔϧ࣮
go/ast ศརʂָ͍͠ʂ 139 139 5. ෳࡶܭଌπʔϧ࣮
ֶͼͱิ 140 140
• ֶͼ ◦ go/ast ͷߏମΛཧղ͢Δͱ͖ͷίπ ▪ Stmt(จ) ͳͲͰྨ͢ΔͱΘ͔Γ͍͢ ▪ Goݴޠ༷ͷϖʔδͰݕࡧͯ͠Θ͔Δ͜ͱʂ
▪ tenntenn ͞Μͷهࣄ܈੩తղੳͷ͍ൣғΛղઆͯ͘͠Ε͍ͯΔ ◦ ੩తղੳπʔϧͷࣗ࡞ʹΑͬͯ go/ast ͱ Goݴޠͷཧղ͕ਂ·ͬͨ ▪ → ʮ੩తղੳπʔϧ͍ͭͰ࡞ΕΔͥʂʯͱੵۃతͳؾ࣋ͪʹ 6. ֶͼͱิ 141
• ิ ◦ golang.org/x/tools/go/analysis Λ͏ͱͬͱศརʂ ▪ ࡞ͬͨπʔϧΛ؆୯ʹఏڙͰ͖Δ(Analyzer) ▪ ͢Ͱʹ͋ΔπʔϧΛ؆୯ʹΈ߹ΘͤΒΕΔ (FuncDecl
ͷϑΟϧλ) ▪ ςετ͕؆୯ʹ͔͚Δ(analysistest) ◦ ͦͷଞͷؔ࿈ϥΠϒϥϦ ▪ golang.org/x/tools/go/ssa • มͷ࠶ೖͳͲΛ͍͔͚Δඞཁ͕͋Δͱ͖ͳͲ ▪ golang.org/x/tools/go/ast/astutil • AST ࣗମʹมߋΛՃ͑Δͱ͖ͳͲ 142 6. ֶͼͱิ
• ิ ◦ golang.org/x/tools/go/analysis Λ͏ͱͬͱศརʂ ▪ ࡞ͬͨπʔϧΛ؆୯ʹఏڙͰ͖Δ(Analyzer) ▪ ͢Ͱʹ͋ΔπʔϧΛ؆୯ʹΈ߹ΘͤΒΕΔ (FuncDecl
ͷϑΟϧλ) ▪ ςετ͕؆୯ʹ͔͚Δ(analysistest) ◦ ͦͷଞͷؔ࿈ϥΠϒϥϦ ▪ golang.org/x/tools/go/ssa • มͷ࠶ೖͳͲΛ͍͔͚Δඞཁ͕͋Δͱ͖ͳͲ ▪ golang.org/x/tools/go/ast/astutil • AST ࣗମʹมߋΛՃ͑Δͱ͖ͳͲ 143 6. ֶͼͱิ
144 6. ֶͼͱิ golang.org/x/tools/go/analysis/analysistest ͷྫ → ςετରͷؔΛ testdata/src/a/a.go ʹஔ͍ͯɺ “//
want “ ίϝϯτΛॻ͚ͩ͘Ͱྑ͍ʂ testdata/src/a/a.go
145 6. ֶͼͱิ golang.org/x/tools/go/analysis/analysistest ͷྫ → ςετରͷؔΛ testdata/src/a/a.go ʹஔ͍ͯɺ “//
want “ ίϝϯτΛॻ͚ͩ͘Ͱྑ͍ʂ testdata/src/a/a.go
·ͱΊ 146 146
• ·ͱΊ ◦ Cognitive Complexity ɺਓؒʹͱͬͯͷෳࡶΛද͢ࢦඪ ◦ go/ast Λ͏͜ͱͰෳࡶͷܭଌπʔϧͳͲͷ੩తղੳπʔϧΛ؆୯ʹ࣮ ͢Δ͜ͱ͕Ͱ͖Δ
▪ golang.org/x/tools/go/analysis ͳͲศར ◦ go/ast ָ͔ͬͨ͠ʂͬͨ͜ͱ͕ͳ͍ਓੋඇͬͯΈΑ͏ʂ 147 7. ·ͱΊ ͓͢͢Ί͍ͨ͠GoϥΠϒϥϦථडத!