Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
人間の直感に対応させた複雑度<Cognitive Complexity>の計測ツールをgo/a...
Search
株式会社ビットキー / Bitkey Inc.
PRO
April 23, 2022
910
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.
デザイナーとして体験を磨き込むためのエンジニアリング / Engineering as a Designer: Polishing the Experience
bitkey
PRO
0
11
スマートロックを支えるFlutter × ネイティブ連携の実践 / Flutter and Native Integration in Practice: Powering Smart Locks
bitkey
PRO
0
150
関数型ドメインモデリングの理想と現実 / Domain Modeling Made Functional: Ideals vs. Reality
bitkey
PRO
0
90
10年後、Engineering 行為を剥奪されるとして、わたしたちは、どう、何を創ればいいだろうか。 / If Engineering Is Taken Away in 10 Years, How and What Should We Create?
bitkey
PRO
1
300
Flutter × BLE Centralを自前Pluginで実装する設計パターン - MethodChannel / EventChannelで作る双方向ブリッジの実践 / Building Custom Flutter BLE Central Plugins: Bidirectional Bridging with Method & Event Channels
bitkey
PRO
0
490
Swift&Xcodeのバージョンアップにまつわる怖かった思い出 / Scary Memories of Swift and Xcode Updates
bitkey
PRO
0
250
iOS/Androidの二刀流エンジニアがFlutter & TypeScriptへ越境後の現在地 - Flutterがメインになって見えた景色と現在の醍醐味 / Dual-Platform Mobile Engineer Shifts to Flutter & TypeScript - The View and Real Thrill of Going Flutter-First
bitkey
PRO
0
690
個人開発で育てる「大規模設計の苗床」 - AI時代の1人開発から始める業務への知識接続 / The Seedbed for Large-Scale Design - From AI-Era Solo Projects to Professional Knowledge
bitkey
PRO
1
630
失敗を経て、Harness Engineering で 大切にしたいことを考える / Learning from Failure: What Matters in Harness Engineering
bitkey
PRO
1
690
Featured
See All Featured
Unsuck your backbone
ammeep
672
58k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
390
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
500
Bash Introduction
62gerente
615
220k
ラッコキーワード サービス紹介資料
rakko
5
5.7M
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.6k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.2k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
620
A better future with KSS
kneath
240
18k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
30 Presentation Tips
portentint
PRO
1
460
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ϥΠϒϥϦථडத!