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 full-size slide

  2. Remixing
    Welcome to

    View full-size slide

  3. Yuya Saito @ Rich Media

    View full-size slide

  4. Refactoring
    Refactoring
    ?
    ?
    ?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  14. Why
    Why
    ͳͥඞཁͳͷ͔

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. Rule of Three
    Rule of Three

    View full-size slide

  31. “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 full-size slide

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

    View full-size slide

  33. Outro
    Outro
    ·ͱΊ

    View full-size slide

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

    View full-size slide

  35. Code smells
    Code smells

    View full-size slide

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

    View full-size slide

  37. Take away
    Take away

    View full-size slide

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

    View full-size slide

  39. The Long Goodbye
    by Raymond Chandler

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  42. Thank You!
    Thank You!
    @cssradar
    @cssradar

    View full-size slide