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

Refactoring: What, Why and When

Refactoring: What, Why and When

リファクタリングとはなんなのか、どうして必要なのか、そしていつ実行するべきなのか。
本セッションではそんなリファクタリングの基礎について紹介していきます。

Yuya Saito

June 21, 2014
Tweet

More Decks by Yuya Saito

Other Decks in Programming

Transcript

  1. What, Why and When
    Refactoring
    Refactoring
    Yuya Saito @ Remixing

    View Slide

  2. Remixing
    Welcome to

    View Slide

  3. Yuya Saito @ Rich Media

    View Slide

  4. Refactoring
    Refactoring
    ?
    ?
    ?

    View Slide

  5. Agenda
    Agenda
    ϦϑΝΫλϦϯάͱ͸ͲΜͳ΋ͷͳͷ͔
    ͳͥϦϑΝΫλϦϯάΛ͢Δඞཁ͕͋Δͷ͔
    ͍ͭ΍Δ΋ͷͳͷ͔
    What
    Why
    When

    View Slide

  6. Refactoring:
    Improving the Design of Existing Code
    by Martin Fowler

    View Slide

  7. What
    What
    ϦϑΝΫλϦϯάͱ͸

    View Slide

  8. “The first draft of
    anything is shit.”
    — Ernest Hemingway

    View Slide

  9. Refactoring (noun): a change made to
    the internal structure of software to
    make it easier to understand and
    cheaper to modify without changing its
    observable behaviour.

    View Slide

  10. ϦϑΝΫλϦϯά ໊ࢺ

    ؍ଌͰ͖ΔৼΔ෣͍Λมߋ͢Δ͜ͱͳ͘ɺ
    ΑΓཧղ͠΍͘͢ɺมߋ͠΍͘͢ͳΔΑ͏ɺ
    ιϑτ΢ΣΞͷ಺෦తͳߏ଄Λมߋ͢Δ͜ͱɻ

    View Slide

  11. ιʔείʔυΛΩϨΠʹ͢Δ͜ͱ
    tl;dr

    View Slide

  12. ཧղ͠΍͘͢ɺ
    Point 1/2
    มߋ͠΍͘͢͢Δɻ
    ιϑτ΢ΣΞΛ

    View Slide

  13. ؍ଌՄೳͳৼΔ෣͍Λมߋ͠ͳ͍
    Point 2/2

    View Slide

  14. Why
    Why
    ͳͥඞཁͳͷ͔

    View Slide

  15. 100%
    100%

    View Slide

  16. 42
    42

    View Slide

  17. Improves the Design of Software
    Refactoring
    Refactoring
    ιϑτ΢ΣΞͷσβΠϯΛ޲্ͤ͞Δ

    View Slide

  18. View Slide

  19. σβΠϯ͸࣌ؒͷܦաʹΑͬͯ
    ඼࣭͕Լ͕͍ͬͯ͘΋ͷ

    View Slide

  20. ϓϩάϥϛϯάͷੈքͰ΋
    ·ͬͨ͘ಉ͜͡ͱ͕ݴ͑Δ

    View Slide

  21. มԽͷڱؒʹஔ͖๨Εͯ͠·ͬͨɺ
    ົͳʹ͓͍͕͢ΔίʔυΛݩʑͷσβΠϯʹ
    ߹Θͤͯॻ͖׵͑ΔͨΊʹଘࡏ͢Δɻ
    Refactoring
    Refactoring

    View Slide

  22. Makes Software Easier to Understand
    Refactoring
    Refactoring
    ιϑτ΢ΣΞΛཧղ͢Δ͜ͱΛखॿ͚ͯ͘͠ΕΔ

    View Slide

  23. Lazy
    Lazy

    View Slide

  24. ॻ͍ͨίʔυʹ͓֮͑ͯ͘΂͖͜ͱ
    ͢΂ͯΛୗ͢ɻ
    ࢒ͨ͠͸ͣͷʮ͓֮͑ͯ͘΂͖ʯ͜ͱΛ
    ಡΈऔΕͳ͍ɻ
    u

    View Slide

  25. ଞਓ͕ॻ͍ͨίʔυ΋ɺ
    ࣗ෼͕ॻ͍ͨίʔυ΋ཧղͰ͖ΔΑ͏
    ॻ͖׵͑Δɻ
    Refactoring
    Refactoring

    View Slide

  26. When
    When
    ͍ͭϦϑΝΫλϦϯάΛ͢Δ΂͖͔

    View Slide

  27. ࣗ෼ͷ͍ͨ৔ॴ͸ɺͦ͜Λग़ͯߦ͘ͱ͖ɺ
    དྷͨ࣌ΑΓ΋͖Ε͍ʹ͠ͳ͚Ε͹ͳΒͳ͍ɻ
    — The Boy Scout Rule

    View Slide

  28. ͲͷϓϩδΣΫτͰ΋ϦϑΝΫλϦϯά͕
    ඞཁͰ͋Δͱ͍͏ೝ͕ࣝ͋Δɻ

    View Slide

  29. Ͳ͏΍ͬͯϦϑΝΫλϦϯάͷ
    ࣌ؒΛ֬อ͢Δ͔
    Problem is:
    Problem is:

    View Slide

  30. Answer is Simple
    Answer is Simple
    ͦΜͳ࣌ؒ͸͍ͭ·Ͱͨͬͯ΋
    ֬อͰ͖ͳ͍ɻ

    View Slide

  31. ن໛ͷେখ͸͋ΕͲιʔείʔυʹ͸
    ඞͣඒ͍͠ͱ͸ݴ͑ͳ͍෦෼͕͋Δ΋ͷɻ

    View Slide

  32. ࣗ෼ͷ͍ͨ৔ॴ͸ɺͦ͜Λग़ͯߦ͘ͱ͖ɺ
    དྷͨ࣌ΑΓ΋͖Ε͍ʹ͠ͳ͚Ε͹ͳΒͳ͍ɻ
    — The Boy Scout Rule

    View Slide

  33. ϦϑΝΫλϦϯά͸ظؒΛ
    ܾΊͯߦ͏΋ͷͰ͸ͳ͍ɻ

    View Slide

  34. Rule of Three
    Rule of Three

    View Slide

  35. 1,2,_,_,_,_

    View Slide

  36. 1,2,3,_,_,_

    View Slide

  37. View Slide

  38. “The first time you do something,
    you just do it. The second time you do
    something similar, you wince at the
    duplication, but you do the duplicate thing
    anyway. The third time you do something
    similar, you refactor.”
    — Don Roberts

    View Slide

  39. ·ͣ࢝Ίͷճ໨ʹԿ͔Λ͢Δ৔߹͸ɺ
    ͨͩͦΕΛ͢Δ͚ͩɻ
    ճ໨ʹԿ͔ࣅͨ͜ͱΛ͢Δ৔߹͸ɺ
    ॏෳʹද৘ΛಶΒͤͳ͕Β΋ɺ
    ͦͷॏෳ͍ͯ͠Δ͜ͱΛ͢Δɻ
    ճ໨ʹͦͷࣅͨԿ͔Λ͢Δࡍɺ
    ϦϑΝΫλΛߦ͏ɻ
    — Don Roberts

    View Slide

  40. Outro
    Outro
    ·ͱΊ

    View Slide

  41. ೏ͬͨΒɺม͑Δɻ
    — The Rule of Baby Rearing

    View Slide

  42. Code smells
    Code smells

    View Slide

  43. ೏͍Λ͔͗෼͚Δೳྗ͕ܦݧͱΑ͹ΕΔ΋ͷ

    View Slide

  44. Take away
    Take away

    View Slide

  45. 1/2

    View Slide

  46. 2/2
    ϦϑΝΫλϦϯά͸
    ظؒΛܾΊͯߦ͏΋ͷͰ͸ͳ͘ɺ
    ௨ৗۀ຿ͷϫʔΫϑϩʔ಺Ͱߦ͏΋ͷͰ͢ɻ

    View Slide

  47. The Long Goodbye
    by Raymond Chandler

    View Slide

  48. “There is no trap so deadly
    as the trap you set for yourself.”
    — Philip Marlowe

    View Slide

  49. “ࣗ෼Ͱࣗ෼ʹ࢓ֻ͚Δ᠘΄Ͳ
    ͨͪͷѱ͍᠘͸ͳ͍ɻ”
    — Philip Marlowe

    View Slide

  50. Thank You!
    Thank You!
    @cssradar
    @cssradar

    View Slide