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

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

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

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

Transcript

  1. Bitkey Inc. Connect Product Noriki Takahashi ਓؒͷ௚ײʹରԠͤͨ͞ෳࡶ౓<Cognitive Complexity>ͷܭଌπʔϧΛgo/ astͰ࣮૷ͯ͠Έͨ

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

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

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

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

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

  7. ಥવͰ͕͢ Έͳ͞Μ 7 7 2. ιʔείʔυͷෳࡶ౓

  8. ෳࡶͳιʔείʔυ͸޷͖Ͱ͔͢ʁ 8 8 2. ιʔείʔυͷෳࡶ౓

  9. ෳࡶͳιʔείʔυ͸޷͖Ͱ͔͢ʁ 9 9 2. ιʔείʔυͷෳࡶ౓

  10. ࢲ͸ͦΜͳʹ޷͖͡Ό͋Γ·ͤΜ 10 10 2. ιʔείʔυͷෳࡶ౓

  11. ࢲ͸ͦΜͳʹ޷͖͡Ό͋Γ·ͤΜ 11 11 2. ιʔείʔυͷෳࡶ౓

  12. Ͱ͖Δ͚ͩγϯϓϧʹͯ͠ อक͠΍͍ͨ͘͢͠ 12 12 2. ιʔείʔυͷෳࡶ౓

  13. ෳࡶ͕͞ආ͚ΒΕͳ͍ͱ͖͸ ςετΛॏ఺తʹߦ͍͍ͨ 13 13 2. ιʔείʔυͷෳࡶ౓

  14. ͜ΕΒΛܧଓతʹ࣮ݱ͍ͯͨ͘͠Ίʹ 14 14 2. ιʔείʔυͷෳࡶ౓

  15. ෳࡶ౓Λఆྔతʹද͍ͨ͠ 15 15 2. ιʔείʔυͷෳࡶ౓

  16. ௐ΂ͨͱ͜Ζ 16 16 2. ιʔείʔυͷෳࡶ౓

  17. ௐ΂ͨͱ͜Ζ 17 17 2. ιʔείʔυͷෳࡶ౓

  18. ਓؒͷײ͡Δෳࡶ͞ʹରԠͨ͠ ࢦඪ͕͋ΔΒ͍͠ 18 18 2. ιʔείʔυͷෳࡶ౓

  19. ͦΕ͕ 19 19 2. ιʔείʔυͷෳࡶ౓

  20. Cognitive Complexity 20 20 2. ιʔείʔυͷෳࡶ౓

  21. Cognitive Complexity 21 21 2. ιʔείʔυͷෳࡶ౓

  22. Cognitive Complexity ͱ͸ 22 22

  23. • Complexity (ෳࡶ౓) ͱ͸ 3. Cognitive Complexityͱ͸ʁ 23

  24. • Complexity (ෳࡶ౓) ͱ͸ ◦ ֊૚͕ਂ͍ͱෳࡶʁ 24 3. Cognitive Complexityͱ͸ʁ

  25. • Complexity (ෳࡶ౓) ͱ͸ ◦ ֊૚͕ਂ͍ͱෳࡶʁ ◦ ෼ذ͕ଟ͍ͱෳࡶʁ 25 3.

    Cognitive Complexityͱ͸ʁ
  26. • Complexity (ෳࡶ౓) ͱ͸ ◦ ֊૚͕ਂ͍ͱෳࡶʁ ◦ ෼ذ͕ଟ͍ͱෳࡶʁ ◦ ίʔυྔ͕ଟ͍ͱෳࡶʁ

    26 3. Cognitive Complexityͱ͸ʁ
  27. • Complexity (ෳࡶ౓) ͱ͸ ◦ ֊૚͕ਂ͍ͱෳࡶʁ ◦ ෼ذ͕ଟ͍ͱෳࡶʁ ◦ ίʔυྔ͕ଟ͍ͱෳࡶʁ

    ◦ ߦ਺͕ଟ͍ͱෳࡶʁ 27 3. Cognitive Complexityͱ͸ʁ
  28. • Complexity (ෳࡶ౓) ͱ͸ ◦ ֊૚͕ਂ͍ͱෳࡶʁ ◦ ෼ذ͕ଟ͍ͱෳࡶʁ ◦ ίʔυྔ͕ଟ͍ͱෳࡶʁ

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

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

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

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

    ▪ Cyclomatic Complexity: 1976೥ 32 White Paper: https://sonarsource.com/docs/CognitiveComplexity.pdf 3. Cognitive Complexityͱ͸ʁ
  33. “ਓؒͷ௚ײͱ͍ۙ” ͱ͸ʁ 33 33 3. Cognitive Complexityͱ͸ʁ

  34. ࣮ྫͰ֬ೝͯ͠ΈΔ 34 34 3. Cognitive Complexityͱ͸ʁ

  35. ͜͜Ͱ࣭໰Ͱ͢ 35 35 3. Cognitive Complexityͱ͸ʁ

  36. ࣍ͷ̎ͭ͸ͲͪΒ͕ෳࡶʁ 36 36 3. Cognitive Complexityͱ͸ʁ

  37. 37 37 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖௚ͨ͠΋ͷ

    Ͳ͕ͬͪෳࡶʁ 3. Cognitive Complexityͱ͸ʁ
  38. Cyclomatic Complexity ͷ৔߹ 38 38 3. Cognitive Complexityͱ͸ʁ

  39. 39 39 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖௚ͨ͠΋ͷ

    3. Cognitive Complexityͱ͸ʁ
  40. 40 40 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖௚ͨ͠΋ͷ

    ಉ͡ʂ 3. Cognitive Complexityͱ͸ʁ
  41. Cognitive Complexity ͷ৔߹ 41 41 3. Cognitive Complexityͱ͸ʁ

  42. 42 42 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖௚ͨ͠΋ͷ

    3. Cognitive Complexityͱ͸ʁ
  43. 43 43 ※ White Paper( https://sonarsource.com/docs/CognitiveComplexity.pdf ) ͷྫΛ Go ʹॻ͖௚ͨ͠΋ͷ

    େࠩʂ 3. Cognitive Complexityͱ͸ʁ
  44. Cognitive Complexity ͍͍Ͷʂ 44 44 3. Cognitive Complexityͱ͸ʁ

  45. 45 ࢀߟ: White Pager (Appendix B) https://sonarsource.com/docs/CognitiveComplexity.pdf • ෳࡶ౓ܭࢉϧʔϧ 3.

    Cognitive Complexityͱ͸ʁ
  46. 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. 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. 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ͱ͸ʁ
  49. • ෳࡶ౓ܭࢉϧʔϧ 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ͱ͸ʁ
  50. • ෳࡶ౓ܭࢉϧʔϧ 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ͱ͸ʁ ωετͷਂ͕͞Ճࢉ஋ʹ
  51. • ෳࡶ౓ܭࢉϧʔϧ 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
  52. Go ͷϓϩάϥϜΛܭଌͯ͠Έ͍ͨʂ 52 52 3. Cognitive Complexityͱ͸ʁ

  53. ੩తղੳπʔϧΛ࡞ΔͨΊͷ ϥΠϒϥϦ͸ͨ͘͞Μ͋Δ 53 53 3. Cognitive Complexityͱ͸ʁ

  54. 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 55 3. Cognitive Complexityͱ͸ʁ

  56. ֓೦͕ଟͯ͘ཧղ͕େม͔΋... 56 56 3. Cognitive Complexityͱ͸ʁ

  57. 57 57 3. Cognitive Complexityͱ͸ʁ ֓೦͕ଟͯ͘ཧղ͕େม͔΋...

  58. େݩʹ͋Δඪ४ͷ go/ast ͚ͩͰ΋े෼࡞ΕΔ 58 58 3. Cognitive Complexityͱ͸ʁ

  59. ੩తղੳͷ࠷ॳͷҰาͱͯ͠ جૅͱͯ͠ go/ast Λཧղ͍ͨ͠...! 59 59 3. Cognitive Complexityͱ͸ʁ

  60. Cognitive Complexity ܭଌπʔϧ go/ast Ͱ࣮૷ͯ͠جຊΛཧղ͠Α͏ 60 60 3. Cognitive Complexityͱ͸ʁ

  61. Cognitive Complexity ܭଌπʔϧ go/ast Ͱ࣮૷ͯ͠جຊΛཧղ͠Α͏ 61 61 3. Cognitive Complexityͱ͸ʁ

  62. go/ast ͱ͸ʁ 62 62

  63. • go/ast ͱ͸ ◦ ߏจ໦Λදݱ͢ΔͨΊʹ࢖༻͞ΕΔܕ͕ೖͬͨύοέʔδ ◦ linter Λ࡞Δͱ͖ͳͲʹར༻͞Ε͍ͯΔ 4. go/ast

    ͱ͸ʁ 63
  64. • go/ast ͱ͸ ◦ ߏจ໦Λදݱ͢ΔͨΊʹ࢖༻͞ΕΔܕ͕ೖͬͨύοέʔδ ◦ linter Λ࡞Δͱ͖ͳͲʹར༻͞Ε͍ͯΔ • AST(ந৅ߏจ໦)ͱ͸

    ◦ ιʔείʔυΛѻ͍΍͍͢Α͏ʹ໦ߏ଄ʹͨ͠΋ͷ ◦ Abstract Syntax Tree ͷུ 4. go/ast ͱ͸ʁ 64
  65. (ந৅)ߏจ໦ͬͯ݁ہͳΜͩ? 65 65 4. go/ast ͱ͸ʁ

  66. ιʔείʔυͷίϯύΠϧ࣌ͷ ྲྀΕͰ֬ೝͯ͠ΈΔ 66 66 4. go/ast ͱ͸ʁ

  67. 67 67 ιʔείʔυ ࣮ߦϑΝΠϧ ίϯύΠϧ࣌ͷॲཧͷྲྀΕ 4. go/ast ͱ͸ʁ ίϯύΠϧ

  68. 68 68 ιʔείʔυ ߏจ໦ ࣮ߦϑΝΠϧ ࣈ۟ղੳ & ߏจղੳ ҙຯղੳ &

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

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

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

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

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

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

    4. go/ast ͱ͸ʁ
  75. • go/ast Ͱදݱ͞ΕΔߏ଄ 75 4. go/ast ͱ͸ʁ

  76. • go/ast Ͱදݱ͞ΕΔߏ଄ 76 Node Node Node Node Node Node

    4. go/ast ͱ͸ʁ
  77. • go/ast Ͱදݱ͞ΕΔߏ଄ 77 Node Node Node Node Node Node

    4. go/ast ͱ͸ʁ ۩ମྫΛ go/ast ͷυΩϡϝϯτͰνΣοΫʂ
  78. 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 ͳͲ
  79. 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 ͳͲ େྔ...! Θ͚͕Θ͔Βͳ͍...
  80. 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 ͳͲ ΜʁΑ͘ݟΔͱ຤ඌ͕ڞ௨͍ͯ͠Δ΍͕ͭ͋Δͳ
  81. 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 ͳͲ
  82. 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ॱʹฒΜͰ͍Δ͕ ߏ଄ମ໊ͷ຤ඌʹ஫໨ͯ͠෼ྨ͢ΔͱΘ͔Γ΍͍͢
  83. 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 ͱ͸ʁ
  84. 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 85 4. go/ast ͱ͸ʁ

  86. 4. go/ast ͱ͸ʁ gopher.go

  87. 4. go/ast ͱ͸ʁ gopher.go main.go : “gopher.go” ͷ AST Λग़ྗ͢Δ

  88. 4. go/ast ͱ͸ʁ gopher.go main.go : “gopher.go” ͷ AST Λग़ྗ͢Δ

    AST ʹม׵ AST Λग़ྗ
  89. File GenDecl FuncDecl Ident BlockStm t CallExpr ImportSpec SelectorExpr BasicLit

    Ident FuncType FieldList BasicLit Ident Ident 4. go/ast ͱ͸ʁ gopher.go
  90. 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
  91. 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
  92. 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
  93. 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
  94. 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
  95. 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 96

  97. • ෳࡶ౓ܭଌπʔϧ ◦ ιʔείʔυ͔Β֤ؔ਺͝ͱͷ Cognitive Complexity Λܭࢉ͢Δ 5. ෳࡶ౓ܭଌπʔϧ࣮૷ 97

  98. • ෳࡶ౓ܭଌπʔϧ ◦ ιʔείʔυ͔Β֤ؔ਺͝ͱͷ Cognitive Complexity Λܭࢉ͢Δ • શମͷߏ଄ 1.

    ιʔείʔυ͔ΒASTΛੜ੒ 2. AST͔Βؔ਺ͷϊʔυͷΈநग़ 3. ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ 98 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  99. ᶃ ιʔείʔυ͔ΒASTΛੜ੒ 99 99 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  100. ᶃ ιʔείʔυ͔ΒASTΛੜ੒ 100 100 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  101. ᶃ ιʔείʔυ͔ΒASTΛੜ੒ 101 101 ϧʔτʹͳΔ Node (File) ͕औಘͰ͖Δ ଞͷNode͸͔͜͜ΒͨͲΔ͜ͱ͕Ͱ͖Δ 5.

    ෳࡶ౓ܭଌπʔϧ࣮૷
  102. ᶄ AST͔Βؔ਺ͷϊʔυͷΈநग़ 102 102 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  103. ᶄ AST͔Βؔ਺ͷϊʔυͷΈநग़ 103 103 ᶃ ͷ File (Node) 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  104. ᶄ AST͔Βؔ਺ͷϊʔυͷΈநग़ 104 104 ίʔυͷதͷؔ਺ͷΈΛऔΓग़͢ → ᶅ Ͱؔ਺ຖʹॲཧ ᶃ ͷ

    File (Node) 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  105. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ 105 105 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  106. 3-1. ෳࡶ౓ͷܭࢉϧʔϧ࠶֬ೝ 106 106 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  107. • ෳࡶ౓ܭࢉϧʔϧ(࠶ܝ) 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. ෳࡶ౓ܭଌπʔϧ࣮૷
  108. 3-2. ॲཧͷશମ૾ 108 108 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  109. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ Visitor Ͱ֤ϊʔυΛ૸ࠪͤ͞Δ 109 109 FuncDecl CallExpr ForStmt

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

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

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

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

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

    ReturnStmt IfStmt BranchStmt ※ ؆ུ൛AST for if continue <label> (+1) (+2) (+1) ෳࡶ౓: 4 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  115. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ 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
  116. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ 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
  117. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ 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
  118. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ؔ਺ຖʹ 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 119 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  120. go/ast ʹؔ਺͕༻ҙ͞Ε͍ͯΔ 120 120 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  121. 3-3. ૸ࠪͷ࣮૷ 121 121 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  122. • ૸ࠪͷ࣮૷ 122 122 ast.Walk ࢦఆͨ͠ node ҎԼΛ visitor Ͱ૸ࠪ

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

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

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

    ast.Visitor ࢦఆ͞Εͨ node ΛVisitͰ૸ࠪ }Node͝ͱʹ ෼ذͯ͠ॲཧ Visit ͷ࣮૷ྫ ͜͜ͷத਎͸ͲΜͳײ͡ʁ 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  126. • ૸ࠪͷ࣮૷ ྫ: ForStmt ༻ͷॲཧ 126 126 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  127. 1 + (ωετͷਂ͞) ͚ͩෳࡶ౓Λ௥Ճ • ૸ࠪͷ࣮૷ ྫ: ForStmt ༻ͷॲཧ 127

    127 5. ෳࡶ౓ܭଌπʔϧ࣮૷
  128. • ૸ࠪͷ࣮૷ ྫ: ForStmt ༻ͷॲཧ ॳظԽɾ৚݅ɾมԽʹཁૉ͕ ͋Ε͹ͦͪΒΛ૸ࠪ (ਂ͞༏ઌ) 128 128

    5. ෳࡶ౓ܭଌπʔϧ࣮૷
  129. • ૸ࠪͷ࣮૷ ྫ: ForStmt ༻ͷॲཧ ωετΛ1ͭਂͯ͘͠ຊମॲཧ ↓ ऴΘͬͨΒωετΛ1ͭ໭͢ 129 129

    5. ෳࡶ౓ܭଌπʔϧ࣮૷
  130. ᶅ ֤ؔ਺಺ͷෳࡶ౓Λܭࢉ ׬ྃʂ 130 130 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  131. ᶃʙᶅΛ૊Έ߹ΘͤΔ 131 131 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  132. 132 132 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  133. 133 133 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  134. 134 134 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  135. 135 135 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  136. ׬੒඼͸ͪ͜Β https://github.com/nrnrk/gocognito 136 136 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  137. ײ૝ 137 137 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  138. go/ast ศརʂָ͍͠ʂ 138 138 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  139. go/ast ศརʂָ͍͠ʂ 139 139 5. ෳࡶ౓ܭଌπʔϧ࣮૷

  140. ֶͼͱิ଍ 140 140

  141. • ֶͼ ◦ go/ast ͷߏ଄ମΛཧղ͢Δͱ͖ͷίπ ▪ Stmt(จ) ͳͲͰ෼ྨ͢ΔͱΘ͔Γ΍͍͢ ▪ Goݴޠ࢓༷ͷϖʔδͰݕࡧͯ͠Θ͔Δ͜ͱ΋ʂ

    ▪ tenntenn ͞Μͷهࣄ܈͸੩తղੳͷ޿͍ൣғΛղઆͯ͘͠Ε͍ͯΔ ◦ ੩తղੳπʔϧͷࣗ࡞ʹΑͬͯ go/ast ͱ Goݴޠͷཧղ͕ਂ·ͬͨ ▪ → ʮ੩తղੳπʔϧ͍ͭͰ΋࡞ΕΔͥʂʯͱੵۃతͳؾ࣋ͪʹ 6. ֶͼͱิ଍ 141
  142. • ิ଍ ◦ 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. ֶͼͱิ଍
  143. • ิ଍ ◦ 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. 144 6. ֶͼͱิ଍ golang.org/x/tools/go/analysis/analysistest ͷྫ → ςετର৅ͷؔ਺Λ testdata/src/a/a.go ʹஔ͍ͯɺ “//

    want “ ίϝϯτΛॻ͚ͩ͘Ͱྑ͍ʂ testdata/src/a/a.go
  145. 145 6. ֶͼͱิ଍ golang.org/x/tools/go/analysis/analysistest ͷྫ → ςετର৅ͷؔ਺Λ testdata/src/a/a.go ʹஔ͍ͯɺ “//

    want “ ίϝϯτΛॻ͚ͩ͘Ͱྑ͍ʂ testdata/src/a/a.go
  146. ·ͱΊ 146 146

  147. • ·ͱΊ ◦ Cognitive Complexity ͸ɺਓؒʹͱͬͯͷෳࡶ౓Λද͢ࢦඪ ◦ go/ast Λ࢖͏͜ͱͰෳࡶ౓ͷܭଌπʔϧͳͲͷ੩తղੳπʔϧΛ؆୯ʹ࣮ ૷͢Δ͜ͱ͕Ͱ͖Δ

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