Share
Embed
人間の直感に対応させた複雑度<Cognitive Complexity>の計測ツールをgo/astで実装してみた
株式会社ビットキー / Bitkey Inc. PRO
April 23, 2022
Transcript
147 slides
-
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͘ΜͷίϥϘʹʂ -
• 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ͱʁ -
37 37 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖ͨ͠ͷ
Ͳ͕ͬͪෳࡶʁ 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ͱʁ -
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ͱʁ -
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/ast ͱ ◦ ߏจΛදݱ͢ΔͨΊʹ༻͞ΕΔܕ͕ೖͬͨύοέʔδ ◦ linter Λ࡞Δͱ͖ͳͲʹར༻͞Ε͍ͯΔ • AST(நߏจ)ͱ
◦ ιʔείʔυΛѻ͍͍͢Α͏ʹߏʹͨ͠ͷ ◦ Abstract Syntax Tree ͷུ 4. go/ast ͱʁ 64 -
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/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 ͕ ͪΌΜͱఆٛ͞Ε͍ͯΔ -
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 -
• ෳࡶܭଌπʔϧ ◦ ιʔείʔυ͔Β֤ؔ͝ͱͷ Cognitive Complexity Λܭࢉ͢Δ • શମͷߏ 1.
ιʔείʔυ͔ΒASTΛੜ 2. AST͔ΒؔͷϊʔυͷΈநग़ 3. ֤ؔͷෳࡶΛܭࢉ 98 5. ෳࡶܭଌπʔϧ࣮ -
ᶄ AST͔ΒؔͷϊʔυͷΈநग़ 104 104 ίʔυͷதͷؔͷΈΛऔΓग़͢ → ᶅ Ͱؔຖʹॲཧ ᶃ ͷ
File (Node) 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. ෳࡶܭଌπʔϧ࣮ -
ᶅ ֤ؔͷෳࡶΛܭࢉ ؔຖʹ 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. ෳࡶܭଌπʔϧ࣮ -
• ࠪͷ࣮ 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. ෳࡶܭଌπʔϧ࣮ -
1 + (ωετͷਂ͞) ͚ͩෳࡶΛՃ • ࠪͷ࣮ ྫ: ForStmt ༻ͷॲཧ 127
127 5. ෳࡶܭଌπʔϧ࣮ -
• ֶͼ ◦ 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 -
• ·ͱΊ ◦ Cognitive Complexity ɺਓؒʹͱͬͯͷෳࡶΛද͢ࢦඪ ◦ go/ast Λ͏͜ͱͰෳࡶͷܭଌπʔϧͳͲͷ੩తղੳπʔϧΛ؆୯ʹ࣮ ͢Δ͜ͱ͕Ͱ͖Δ
▪ golang.org/x/tools/go/analysis ͳͲศར ◦ go/ast ָ͔ͬͨ͠ʂͬͨ͜ͱ͕ͳ͍ਓੋඇͬͯΈΑ͏ʂ 147 7. ·ͱΊ ͓͢͢Ί͍ͨ͠GoϥΠϒϥϦථडத!