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

Well-organized Transaction Script - リファクタリングの妥協的手法 -

qsona
June 14, 2022

Well-organized Transaction Script - リファクタリングの妥協的手法 -

qsona

June 14, 2022
Tweet

More Decks by qsona

Other Decks in Technology

Transcript

  1. Well-organized Transaction Script
    - ϦϑΝΫλϦϯάͷଥڠతख๏ -
    @qsona
    2022-06-24 ઃܭφΠτ2022 ʮτϥϯβΫγϣϯεΫϦϓτʯ
    #sekkeinight

    View full-size slide

  2. whoami
    • @qsona
    • Software Engineer at ελσΟαϓϦ (ϦΫϧʔτ)
    • Node.js / Rails / GraphQL / Microservices

    View full-size slide

  3. ࠓ೔ͷ͋Β͢͡
    • 1. ࠷ۙνʔϜͰߦΘΕͨ

    Transaction Script ͷϦϑΝΫλϦϯάͷ࿩
    • 2. Transaction Script ͷϦϑΝΫλϦϯάํ਑ʹ͍ͭͯߟ࡯

    View full-size slide

  4. ࠷ۙνʔϜͰߦΘΕͨ

    Transaction Script ͷϦϑΝΫλϦϯά

    View full-size slide

  5. https://studysapuri.jp/course/junior/release-2022-spring/

    View full-size slide

  6. ࠓ೔ͷ࿩ͷίϯςΩετ
    • ελσΟαϓϦʹ͓͚ΔϚελʔσʔλ
    • ֶशऀֶ͕श͢ΔͨΊͷ໰୊΍ಈըͱɺ

    ͦΕΒΛ֊૚ߏ଄ʹ·ͱΊͨ΋ͷ
    • ʮίϯςϯπʯͱݺΜͰ͍Δ
    • (ฐνʔϜͰ͸) YAML, CSV ͳͲͰ؅ཧ͞Ε͍ͯΔ
    • ͜ΕΛσʔλϕʔεʹ౤ೖ͢ΔεΫϦϓτͷ࿩

    View full-size slide

  7. ίϯςϯπΠϯϙʔτͷίʔυ֓ཁ
    • େ͖͘ҎԼͷ3ͭͷؔ਺ʹ෼͔Εɺॱʹ࣮ߦ͞ΕΔ
    • 1. processContentData ...

    CSV, YAML Ͱߏ੒͞Εͨ

    ίϯςϯπσʔλΛશͯparse͠ɺ

    1ຕͷ JS Object ʹม׵͢Δ
    • 2. deleteAllContents ... DB ্ͷίϯςϯπΛશ࡟আ
    • 3. insertContents ... 1 Ͱ࡞ͬͨσʔλΛ DB ʹ౤ೖ͢Δ
    ໿ 700 ߦ, ࠷ਂ 4 ॏϧʔϓ͕ग़ݱ͢Δ
    τϥϯβΫγϣϯεΫϦϓτɻ
    ίʔυͷೝ஌ෛՙ͕ߴ͍

    View full-size slide

  8. τϥϯβΫγϣϯεΫϦϓτͷ՝୊
    • Ͳ͜ͰԿͷॲཧΛ΍͍ͬͯΔ͔͕ύοͱΘ͔Βͳ͍
    • ϧʔϓ͕ଟॏʹͳ͍ͬͯͯɺม਺ͷείʔϓ͕෼͔Γʹ͍͘
    • => ίʔυͷೝ஌ෛՙ͕ߴ͘ɺॳݟࡴ͠
    ϦϑΝΫλϦϯά͍ͨ͠

    View full-size slide

  9. ϦϑΝΫλϦϯάͷํ਑?
    • ʮυϝΠϯʯΛߟ࡯ͯ͠
    • ʮυϝΠϯϞσϧʯΛද͢ΫϥεΛ࡞ͬͯ
    • ͦΕΒͷϝιουΛݺΜͰ౷߹͢Δײ͡...?

    View full-size slide

  10. ٙ໰
    • ߟ࡯͢Δ΄ͲͷʮυϝΠϯʯ͕ଘࡏ͠ͳͦ͏
    • ิ଍: ίϯςϯπͷ࡞੒ʙೖߘ͸ߟ࡯͕͍͠ͷ͋ΔυϝΠϯͷๅݿͳͷ͕ͩɺ

    ͜ͷ YAML, CSV ΛΠϯϙʔτ͢ΔՕॴ͚ͩΛݟΔͱ͋·Γͳ͍ͱ͍͏ҙຯ
    • TypeScript ͰΫϥε͋Μ·Γ࢖͍ͨ͘ͳ͍

    View full-size slide

  11. ϦϑΝΫλϦϯάͷํ਑ɾվ
    • Ұ୴ɺͦΕͬΆཻ͍౓ͷؔ਺ʹ෼ׂ͍ͯ͜͠͏

    View full-size slide

  12. యܕత
    τϥϯβΫγϣϯεΫϦϓτ

    View full-size slide

  13. ͜Μͳײ͡ʹ
    ෳ਺ͷॲཧʹ෼ׂ͢Δ
    ͜ΕΛҾ͖ଓ͖ਐΊ͍ͯ͘

    View full-size slide

  14. ݁Ռ
    • ී௨ʹ͏·͍͘͘ (ຊ౰ʹී௨Ͱ͢Ͷɾɾɾ)
    • before: Ͳ͜ͰԿͷॲཧΛ΍͍ͬͯΔ͔͕ύοͱΘ͔Βͳ͍

    => after: ؔ਺ʹ෼ׂ͞Εɺ໊લ͕෇͚ΒΕͯɺΘ͔Γ΍͘͢ͳͬͨ
    • before: ϧʔϓ͕ଟॏʹͳ͍ͬͯͯม਺ͷείʔϓ͕෼͔Γʹ͍͘

    => after: ֤ϧʔϓ಺ͷॲཧ͕ؔ਺ʹͳͬͨͷͰɺείʔϓ͸Θ͔Γ΍͘͢ͳͬͨ
    • ෼ׂͨ͠୯ҐͰ୯ମςετ΋ॻ͚ΔΑ͏ʹ

    View full-size slide

  15. ϙΠϯτ
    • ୯ʹಡΈ΍͘͢ͳͬͨ & 

    มߋ͍ͨ͠ͱ͖ʹมߋ͢΂͖Օॴ͕Θ͔Γ΍͘͢ͳ͚ͬͨͩ
    • ΞʔΩςΫνϟʹຊ࣭తͳมԽ͸ͳ͍
    • ͕ɺ໨ઌͷ՝୊͸ղܾͨ͠ͱ͍͏࿩

    View full-size slide

  16. Transaction Script ͷ
    ϦϑΝΫλϦϯάํ਑ʹ͍ͭͯߟ࡯

    View full-size slide

  17. Transaction Script ͱ͸
    • ͜͜Ͱ͸ʮಛఆͷϢʔεέʔε͕ͦͷ··දݱ͞Εͨίʔυʯ
    ͱଊ͑Δ
    ͍·͞Β͖͚ͳ͍ʮυϝΠϯϞσϧʯͱʮτϥϯβΫγϣϯεΫϦϓτʯ - ͻ͕΍͢Λٕज़ϒϩά

    View full-size slide

  18. ϦϑΝΫλϦϯάԦಓϧʔτ (ϘτϜΞοϓܕ)
    • υϝΠϯΛߟ࡯͠ɺυϝΠϯϞσϧΛ࡞Δ
    • ͦͷυϝΠϯϞσϧΛϢʔεέʔεͰར༻͢Δ

    View full-size slide

  19. ϦϑΝΫλϦϯάଥڠϧʔτ (τοϓμ΢ϯܕ)
    • Lv.0 Ϣʔεέʔε͕΂ͬͨΓ௕ͬͨΒ͘͠ίʔυͰදݱ͞Ε͍ͯΔ

    (యܕతͳτϥϯβΫγϣϯεΫϦϓτ)
    • Lv.1 ؔ਺܈ʹ෼ղ͢Δ (Composed Method ύλʔϯ)
    • Lv.2 ؔ਺܈Λ public ʹͯ͠ɺͦΕͧΕςετΛॻ͘

    (Ծশ: Well-organized Transaction Script)
    • Lv.3 ֤ؔ਺܈ʹυϝΠϯϞσϧͱͯ͠ͷৼΔ෣͍Λ࣋ͨͤΔ

    View full-size slide

  20. Lv.2 (Well-organized Transaction Script)
    export Λ͚͍ͭͯΔ "͚ͩ" ͕ͩɺ
    ͦΕʹ͸Ұఆͷઃܭ൑அΛཁ͢Δ

    View full-size slide

  21. Well-organized Transaction Script
    • ͋͘·ͰϢʔεέʔεΛ෼ղ͍ͯ͠Δ͚ͩͰɺ

    ຊ࣭తͳมԽ͸ى͖͍ͯͳ͍ (υϝΠϯϞσϧͰ͸ͳ͍)
    • ؔ਺ཻ͕౓Λߟ͑ͯ෼ղ͞Εɺ୯ମςετՄೳʹͳ͍ͬͯΔɻ

    ֤ؔ਺͸ɺࠓޙυϝΠϯϞσϧʹͳΔީิʹͳ͍ͬͯΔ

    (Lv.3΁ͷมԽ)

    View full-size slide

  22. ࢲݟ: Lv.1 ͰࢭΊͪΌ͏ਓଟ͍
    • ʮ͜ͷϝιου͸͜ͷϢʔεέʔε͔Β͔͠࢖ΘΕͳ͍͔Β
    private ϝιουʹ͠·͢ʯ=> ؒҧͬͯ͸ͳ͍͕
    • 1ͭͷϢʔεέʔε͔Β͔͠࢖ΘΕͳͯ͘΋ public Ͱྑ͍
    • public ʹ͢Δͱಉ࣌ʹɺྑ͍ΠϯλʔϑΣΠεΛҙࣝ͢Δɻ

    ݁Ռςετ΋͠΍͘͢ͳΔɻ

    View full-size slide

  23. ͍ͭଥڠϧʔτΛબͿͷ͔
    • υϝΠϯϞσϧΛߟ࡯͢Δҙຯ͕ബ͍ͱ͖
    • υϝΠϯϞσϧΛຊ֨తʹߟ࡯͢Δೳྗ·ͨ͸͕࣌ؒͳ͍ͱ͖
    • ͦΕͰ΋ஈ֊తʹલʹਐΉ͜ͱ͕Ͱ͖Δ

    View full-size slide

  24. ͓·͚: υϝΠϯϞσϧͷදݱํ๏
    • ݴޠಛੑʹΑͬͯදݱํ๏͸มΘΓ͏Δ
    • TypeScript Ͱ͸ΫϥεΛ࣋ͪग़͞ͳ͍΄͏͕͏·͍͖͘΍͍͢
    • σϑΥϧτ͸ؔ਺ϕʔεͰɻΫϥε͸ಛผͳ࣌ʹɻ

    View full-size slide

  25. ຊൃදͷ·ͱΊ
    • τϥϯβΫγϣϯεΫϦϓτΛϦϑΝΫλϦϯά͢Δͱ͖ɺ

    ࠷ॳ͔ΒυϝΠϯϞσϧΛߟ࡯͢Δख๏ (Ԧಓϧʔτ) ͱ

    Ұ୴ϢʔεέʔεΛ෼ղ͍ͯ͘͠ख๏ (ଥڠϧʔτ) ͕͋Δ
    • ଥڠϧʔτ͔ΒͰ΋ɺ࠷ऴతʹυϝΠϯϞσϧ͕ߟ࡯͞Εͨঢ়ଶ
    ʹొΔ͜ͱ͕Ͱ͖Δ
    • ͦ΋ͦ΋υϝΠϯΛߟ࡯͢Δҙຯ͕ബ͍ͱ͖΋͋ΔΑ

    View full-size slide

  26. എܠʹڵຯ΋ͬͯԼͬͨ͞ํ΁
    • ͜ͷ୊ࡐͱͳͬͨτϥϯβΫγϣϯεΫϦϓτ

    ͕ੜ·ΕͨഎܠͳͲ͸

    jsconfjp 2021 Ͱొஃͨ͠ࢿྉ (ӈ) Λ

    ͥͻ͝ࢀর͍ͩ͘͞
    • ελσΟαϓϦͰ͸ɺڭҭ/ֶशυϝΠϯʹڵຯͷ͋Δ

    ΤϯδχΞΛืू͍ͯ͠·͢

    (ڵຯ͋Δํ Twitter @qsona ʹϝϯγϣϯ΍DM͍ͩ͘͞)

    View full-size slide

  27. కΊͷҰ۟
    • P of EAA 2002೥ Զͨͪੜ͖ͯΔ 2022೥

    View full-size slide