Upgrade to Pro — share decks privately, control downloads, hide ads and more …

人間の直感に対応させた複雑度<Cognitive Complexity>の計測ツールをgo/a...

人間の直感に対応させた複雑度<Cognitive Complexity>の計測ツールをgo/astで実装してみた

More Decks by 株式会社ビットキー / Bitkey Inc.

Transcript

  1. Outline 1. ࣗݾ঺հ 2. ιʔείʔυͷෳࡶ౓ 3. Cognitive Complexityͱ͸ʁ 4. go/ast

    ͱ͸ʁ 5. ෳࡶ౓ܭଌπʔϧ࣮૷ 6. ֶͼͱิ଍ 7. ·ͱΊ 2 2
  2. 1. ࣗݾ঺հ 3 • ∁ڮయੜ(Noriki Takahashi) ◦ Twitter: @norinoriki0 ◦

    GitHub: nrnrk • גࣜձࣾBitkey ◦ bitkey platform ͱ͍͏”ΧΪ”ͷϓϥοτϑΥʔϜͷ։ൃ • Go Ͱ։ൃ͍ͯ͠·͕͢ɺ go/ast ͸͜Ε·Ͱ৮ͬͨ͜ͱ͕͋Γ·ͤΜͰͨ͠ • झຯ: ਺ֶɾ෺ཧֶ ◦ zk-SNARKs ͷ਺ֶతͳଆ໘ΛטΈࡅ͘هࣄΛॻ͍ͨΓͯ͠·͢ • #gocon Ͱײ૝ͳͲੋඇΑΖ͓͘͠Ͷ͕͍͠·͢ʂ
  3. • Bitkey ͷ3ͭͷࣄۀυϝΠϯ ◦ Homeʢ฻Β͠ʣ ◦ Workspaceʢಇ͘ʣ ◦ Experienceʢඇ೔ৗͷମݧʣ •

    bitkey platform ◦ 3ͭͷ hub ΞϓϦέʔγϣϯͷج൫ 1. ࣗݾ঺հ 4 ςΫϊϩδʔͷྗͰ͋ΒΏΔ΋ͷΛ ҆શͰ ศརͰ ؾ࣋ͪΑ͘ʮͭͳ͛Δʯ ͓͢͢Ί͍ͨ͠GoϥΠϒϥϦ౤ථड෇த!
  4. • Bitkey ͷ3ͭͷࣄۀυϝΠϯ ◦ Homeʢ฻Β͠ʣ ◦ Workspaceʢಇ͘ʣ ◦ Experienceʢඇ೔ৗͷମݧʣ •

    bitkey platform ◦ 3ͭͷ hub ΞϓϦέʔγϣϯͷج ൫ 1. ࣗݾ঺հ 5 ςΫϊϩδʔͷྗͰ͋ΒΏΔ΋ͷΛ ҆શͰ ศརͰ ؾ࣋ͪΑ͘ʮͭͳ͛Δʯ ͓͢͢Ί͍ͨ͠GoϥΠϒϥϦ౤ථड෇த! എܠͷ bitkey x Gopher͘ΜͷίϥϘʹ΋஫໨ʂ
  5. • Complexity (ෳࡶ౓) ͱ͸ ◦ ֊૚͕ਂ͍ͱෳࡶʁ ◦ ෼ذ͕ଟ͍ͱෳࡶʁ ◦ ίʔυྔ͕ଟ͍ͱෳࡶʁ

    ◦ ߦ਺͕ଟ͍ͱෳࡶʁ → ஫໨͢ΔϙΠϯτʹΑͬͯࢦඪ͕ҟͳΔ 28 3. Cognitive Complexityͱ͸ʁ
  6. • ༷ʑͳෳࡶ౓ͷࢦඪ ◦ ॥؀తෳࡶ౓ (Cyclomatic Complexity) ▪ ઢܗతʹಠཱͨ͠ܦ࿏ͷ਺ʹ஫໨ͨ͠ࢦඪ ▪ (ͬ͘͟Γ͍͏ͱ)

    ܦ࿏ͷύλʔϯ͕ଟ͍ͱෳࡶͱ൑ఆ ◦ Halstead ෳࡶ౓ ▪ ௕͞΍“ޠኮ”ͷ਺ʹ஫໨ͨ͠ࢦඪ ▪ (ͬ͘͟Γ͍͏ͱ) ௕͍΋ͷ΍දݱύλʔϯ͕ଟ͍ͱෳࡶͱ൑ఆ 29 3. Cognitive Complexityͱ͸ʁ
  7. • ༷ʑͳෳࡶ౓ͷࢦඪ ◦ ॥؀తෳࡶ౓ (Cyclomatic Complexity) ▪ ઢܗతʹಠཱͨ͠ܦ࿏ͷ਺ʹ஫໨ͨ͠ࢦඪ ▪ (ͬ͘͟Γ͍͏ͱ)

    ܦ࿏ͷύλʔϯ͕ଟ͍ͱෳࡶͱ൑ఆ ◦ Halstead ෳࡶ౓ ▪ ௕͞΍“ޠኮ”ͷ਺ʹ஫໨ͨ͠ࢦඪ ▪ (ͬ͘͟Γ͍͏ͱ) ௕͍΋ͷ΍දݱύλʔϯ͕ଟ͍ͱෳࡶͱ൑ఆ 30 3. Cognitive Complexityͱ͸ʁ ༗໊ʂ਺ֶతͳϞσϧʹجͮ͘ʂ
  8. • ೝ஌తෳࡶ౓ (Cognitive Complexity) ◦ ਓؒͷ௚ײͱ͍ۙෳࡶ͞Λ໨ࢦͨ͠ࢦඪ ◦ ίʔυͷઢܗͷྲྀΕΛཚ͢΋ͷʹ஫໨ ◦ 2016೥ʹ஀ੜ

    ▪ Cyclomatic Complexity: 1976೥ 31 White Paper: https://sonarsource.com/docs/CognitiveComplexity.pdf 3. Cognitive Complexityͱ͸ʁ
  9. • ೝ஌తෳࡶ౓ (Cognitive Complexity) ◦ ਓؒͷ௚ײͱ͍ۙෳࡶ͞Λ໨ࢦͨ͠ࢦඪ ◦ ίʔυͷઢܗͷྲྀΕΛཚ͢΋ͷʹ஫໨ ◦ 2016೥ʹ஀ੜ

    ▪ Cyclomatic Complexity: 1976೥ 32 White Paper: https://sonarsource.com/docs/CognitiveComplexity.pdf 3. Cognitive Complexityͱ͸ʁ
  10. 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ͱ͸ʁ
  11. 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ͱ͸ʁ
  12. 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ͱ͸ʁ
  13. • ෳࡶ౓ܭࢉϧʔϧ 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ͱ͸ʁ
  14. • ෳࡶ౓ܭࢉϧʔϧ 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ͱ͸ʁ ωετͷਂ͕͞Ճࢉ஋ʹ
  15. • ෳࡶ౓ܭࢉϧʔϧ 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
  16. • go/ast ͱ͸ ◦ ߏจ໦Λදݱ͢ΔͨΊʹ࢖༻͞ΕΔܕ͕ೖͬͨύοέʔδ ◦ linter Λ࡞Δͱ͖ͳͲʹར༻͞Ε͍ͯΔ • AST(ந৅ߏจ໦)ͱ͸

    ◦ ιʔείʔυΛѻ͍΍͍͢Α͏ʹ໦ߏ଄ʹͨ͠΋ͷ ◦ Abstract Syntax Tree ͷུ 4. go/ast ͱ͸ʁ 64
  17. 68 68 ιʔείʔυ ߏจ໦ ࣮ߦϑΝΠϧ ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ &

    ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱ͸ʁ
  18. 69 69 ιʔείʔυ ߏจ໦ ࣮ߦϑΝΠϧ func main() { result :=

    `apple` + `pen` … } ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ & ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱ͸ʁ
  19. 70 70 ιʔείʔυ ߏจ໦ ࣮ߦϑΝΠϧ func main() { result :=

    `apple` + `pen` … } ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ & ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱ͸ʁ apple pen +
  20. 71 71 ιʔείʔυ ߏจ໦ ࣮ߦϑΝΠϧ func main() { result :=

    `apple` + `pen` … } ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ & ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱ͸ʁ apple pen +
  21. 72 72 ιʔείʔυ ߏจ໦ ࣮ߦϑΝΠϧ func main() { result :=

    `apple` + `pen` … } ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ & ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱ͸ʁ apple pen +
  22. 73 73 ιʔείʔυ ߏจ໦ ࣮ߦϑΝΠϧ func main() { result :=

    `apple` + `pen` … } ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ & ࠷దԽ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ ߏ଄Խ͞Ε͍ͯΔ͓͔͛Ͱ... ίʔυΛνΣοΫ͢Δॲཧ͕ॻ͖΍͍͢ (ྫ: ه๏ͷνΣοΫɺύοέʔδґଘͳͲ) AST = go/ast Ͱ͜ͷߏ଄Λར༻Ͱ͖ Δ 4. go/ast ͱ͸ʁ apple pen +
  23. • go/ast Ͱදݱ͞ΕΔߏ଄ 77 Node Node Node Node Node Node

    4. go/ast ͱ͸ʁ ۩ମྫΛ go/ast ͷυΩϡϝϯτͰνΣοΫʂ
  24. 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 ͳͲ
  25. 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 ͳͲ େྔ...! Θ͚͕Θ͔Βͳ͍...
  26. 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 ͳͲ ΜʁΑ͘ݟΔͱ຤ඌ͕ڞ௨͍ͯ͠Δ΍͕ͭ͋Δͳ
  27. 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 ͳͲ
  28. 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ॱʹฒΜͰ͍Δ͕ ߏ଄ମ໊ͷ຤ඌʹ஫໨ͯ͠෼ྨ͢ΔͱΘ͔Γ΍͍͢
  29. 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 ͱ͸ʁ
  30. 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 ͕ ͪΌΜͱఆٛ͞Ε͍ͯΔ
  31. File GenDecl FuncDecl Ident BlockStm t CallExpr ImportSpec SelectorExpr BasicLit

    Ident FuncType FieldList BasicLit Ident Ident 4. go/ast ͱ͸ʁ gopher.go
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. • ෳࡶ౓ܭଌπʔϧ ◦ ιʔείʔυ͔Β֤ؔ਺͝ͱͷ Cognitive Complexity Λܭࢉ͢Δ • શମͷߏ଄ 1.

    ιʔείʔυ͔ΒASTΛੜ੒ 2. AST͔Βؔ਺ͷϊʔυͷΈநग़ 3. ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ 98 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  39. • ෳࡶ౓ܭࢉϧʔϧ(࠶ܝ) 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. ෳࡶ౓ܭଌπʔϧ࣮૷
  40. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ Visitor Ͱ֤ϊʔυΛ૸ࠪͤ͞Δ 109 109 FuncDecl CallExpr ForStmt

    ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  41. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ Visitor Ͱ֤ϊʔυΛ૸ࠪͤ͞Δ 110 110 FuncDecl CallExpr ForStmt

    ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  42. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ Visitor Ͱ֤ϊʔυΛ૸ࠪͤ͞Δ 111 111 FuncDecl CallExpr ForStmt

    ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  43. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ Visitor Ͱ֤ϊʔυΛ૸ࠪͤ͞Δ 112 112 FuncDecl CallExpr ForStmt

    ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  44. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ Visitor Ͱ֤ϊʔυΛ૸ࠪͤ͞Δ 113 113 FuncDecl CallExpr ForStmt

    ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) (+1) 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  45. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ Visitor Ͱ֤ϊʔυΛ૸ࠪͤ͞Δ 114 114 FuncDecl CallExpr ForStmt

    ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) (+1) ෳࡶ౓: 4 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  46. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ 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
  47. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ 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
  48. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ 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
  49. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ 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. ෳࡶ౓ܭଌπʔϧ࣮૷
  50. • ૸ࠪͷ࣮૷ 122 122 ast.Walk ࢦఆͨ͠ node ҎԼΛ visitor Ͱ૸ࠪ

    ast.Visitor ࢦఆ͞Εͨ node ΛVisitͰ૸ࠪ 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  51. • ૸ࠪͷ࣮૷ 123 123 ast.Walk ࢦఆͨ͠ node ҎԼΛ visitor Ͱ૸ࠪ

    ast.Visitor ࢦఆ͞Εͨ node ΛVisitͰ૸ࠪ Visit ͷ࣮૷ྫ 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  52. • ૸ࠪͷ࣮૷ 124 124 ast.Walk ࢦఆͨ͠ node ҎԼΛ visitor Ͱ૸ࠪ

    ast.Visitor ࢦఆ͞Εͨ node ΛVisitͰ૸ࠪ }Node͝ͱʹ ෼ذͯ͠ॲཧ Visit ͷ࣮૷ྫ 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  53. • ૸ࠪͷ࣮૷ 125 125 ast.Walk ࢦఆͨ͠ node ҎԼΛ visitor Ͱ૸ࠪ

    ast.Visitor ࢦఆ͞Εͨ node ΛVisitͰ૸ࠪ }Node͝ͱʹ ෼ذͯ͠ॲཧ Visit ͷ࣮૷ྫ ͜͜ͷத਎͸ͲΜͳײ͡ʁ 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  54. • ֶͼ ◦ go/ast ͷߏ଄ମΛཧղ͢Δͱ͖ͷίπ ▪ Stmt(จ) ͳͲͰ෼ྨ͢ΔͱΘ͔Γ΍͍͢ ▪ Goݴޠ࢓༷ͷϖʔδͰݕࡧͯ͠Θ͔Δ͜ͱ΋ʂ

    ▪ tenntenn ͞Μͷهࣄ܈͸੩తղੳͷ޿͍ൣғΛղઆͯ͘͠Ε͍ͯΔ ◦ ੩తղੳπʔϧͷࣗ࡞ʹΑͬͯ go/ast ͱ Goݴޠͷཧղ͕ਂ·ͬͨ ▪ → ʮ੩తղੳπʔϧ͍ͭͰ΋࡞ΕΔͥʂʯͱੵۃతͳؾ࣋ͪʹ 6. ֶͼͱิ଍ 141
  55. • ิ଍ ◦ 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. ֶͼͱิ଍
  56. • ิ଍ ◦ 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. ֶͼͱิ଍
  57. • ·ͱΊ ◦ Cognitive Complexity ͸ɺਓؒʹͱͬͯͷෳࡶ౓Λද͢ࢦඪ ◦ go/ast Λ࢖͏͜ͱͰෳࡶ౓ͷܭଌπʔϧͳͲͷ੩తղੳπʔϧΛ؆୯ʹ࣮ ૷͢Δ͜ͱ͕Ͱ͖Δ

    ▪ golang.org/x/tools/go/analysis ͳͲ΋ศར ◦ go/ast ָ͔ͬͨ͠ʂ࢖ͬͨ͜ͱ͕ͳ͍ਓ͸ੋඇ࢖ͬͯΈΑ͏ʂ 147 7. ·ͱΊ ͓͢͢Ί͍ͨ͠GoϥΠϒϥϦ౤ථड෇த!