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
株式会社ビットキー / Bitkey Inc.
April 23, 2022
0
800
人間の直感に対応させた複雑度<Cognitive Complexity>の計測ツールをgo/astで実装してみた
株式会社ビットキー / Bitkey Inc.
April 23, 2022
Tweet
Share
More Decks by 株式会社ビットキー / Bitkey Inc.
See All by 株式会社ビットキー / Bitkey Inc.
整頓のジレンマとの戦い〜Tidy First?で振り返る事業とキャリアの歩み〜/Fighting the tidiness dilemma〜Business and Career Milestones Reflected on in Tidy First?〜
bitkey
2
15k
遠隔で同時に4人教育!? 〜リモート新人の「孤立」を防ぐオンボーディング事例〜/Educating 4 people at the same time remotely!〜Onboarding Case Studies to Prevent “Isolation” of Remote Newcomers〜
bitkey
0
47
ファームウェアは土管になるべきか 翻訳機になるべきか/Should firmware be a clay pipe or a translator?
bitkey
0
47
テストは合意形成をすべし!/Testing should be consensus building!
bitkey
0
63
Agentic Coding と 山登り と TypeScript〜体系的に考えてみよう!〜/Agentic Coding, Mountain Climbing and TypeScript~Let's think systematically!~
bitkey
0
250
"発信文化"をどうやって計測する?技術広報のKPI探索記/How do we measure communication culture?
bitkey
4
630
MCP でモノが動くとおもしろい/It is interesting when things move with MCP
bitkey
3
760
カオスに立ち向かう小規模チームの装備の選択〜フルスタックTSという装備の強み _ 弱み〜/Choosing equipment for a small team facing chaos ~ Strengths and weaknesses of full-stack TS~
bitkey
1
210
型化からはじめる時間づくり/Creating Time Starting with Modeling
bitkey
1
110
Featured
See All Featured
Speed Design
sergeychernyshev
32
1k
GitHub's CSS Performance
jonrohan
1031
460k
Docker and Python
trallard
44
3.5k
The Cost Of JavaScript in 2023
addyosmani
51
8.5k
RailsConf 2023
tenderlove
30
1.1k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Music & Morning Musume
bryan
46
6.6k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.9k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Statistics for Hackers
jakevdp
799
220k
Designing for humans not robots
tammielis
253
25k
Designing Experiences People Love
moore
142
24k
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ϥΠϒϥϦථडத!