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

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

942bb606679caf4c57b38927f83178e1?s=47 qsona
June 14, 2022

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

942bb606679caf4c57b38927f83178e1?s=128

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
  2. whoami • @qsona • Software Engineer at ελσΟαϓϦ (ϦΫϧʔτ) •

    Node.js / Rails / GraphQL / Microservices
  3. ࠓ೔ͷ͋Β͢͡ • 1. ࠷ۙνʔϜͰߦΘΕͨ
 Transaction Script ͷϦϑΝΫλϦϯάͷ࿩ • 2. Transaction

    Script ͷϦϑΝΫλϦϯάํ਑ʹ͍ͭͯߟ࡯
  4. ࠷ۙνʔϜͰߦΘΕͨ
 Transaction Script ͷϦϑΝΫλϦϯά

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

  7. ࠓ೔ͷ࿩ͷίϯςΩετ • ελσΟαϓϦʹ͓͚ΔϚελʔσʔλ • ֶशऀֶ͕श͢ΔͨΊͷ໰୊΍ಈըͱɺ
 ͦΕΒΛ֊૚ߏ଄ʹ·ͱΊͨ΋ͷ • ʮίϯςϯπʯͱݺΜͰ͍Δ • (ฐνʔϜͰ͸)

    YAML, CSV ͳͲͰ؅ཧ͞Ε͍ͯΔ • ͜ΕΛσʔλϕʔεʹ౤ೖ͢ΔεΫϦϓτͷ࿩
  8. ίϯςϯπΠϯϙʔτͷίʔυ֓ཁ • େ͖͘ҎԼͷ3ͭͷؔ਺ʹ෼͔Εɺॱʹ࣮ߦ͞ΕΔ • 1. processContentData ...
 CSV, YAML Ͱߏ੒͞Εͨ


    ίϯςϯπσʔλΛશͯparse͠ɺ
 1ຕͷ JS Object ʹม׵͢Δ • 2. deleteAllContents ... DB ্ͷίϯςϯπΛશ࡟আ • 3. insertContents ... 1 Ͱ࡞ͬͨσʔλΛ DB ʹ౤ೖ͢Δ ໿ 700 ߦ, ࠷ਂ 4 ॏϧʔϓ͕ग़ݱ͢Δ τϥϯβΫγϣϯεΫϦϓτɻ ίʔυͷೝ஌ෛՙ͕ߴ͍
  9. τϥϯβΫγϣϯεΫϦϓτͷ՝୊ • Ͳ͜ͰԿͷॲཧΛ΍͍ͬͯΔ͔͕ύοͱΘ͔Βͳ͍ • ϧʔϓ͕ଟॏʹͳ͍ͬͯͯɺม਺ͷείʔϓ͕෼͔Γʹ͍͘ • => ίʔυͷೝ஌ෛՙ͕ߴ͘ɺॳݟࡴ͠ ϦϑΝΫλϦϯά͍ͨ͠

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

  11. ٙ໰ • ߟ࡯͢Δ΄ͲͷʮυϝΠϯʯ͕ଘࡏ͠ͳͦ͏ • ิ଍: ίϯςϯπͷ࡞੒ʙೖߘ͸ߟ࡯͕͍͠ͷ͋ΔυϝΠϯͷๅݿͳͷ͕ͩɺ
 ͜ͷ YAML, CSV ΛΠϯϙʔτ͢ΔՕॴ͚ͩΛݟΔͱ͋·Γͳ͍ͱ͍͏ҙຯ

    • TypeScript ͰΫϥε͋Μ·Γ࢖͍ͨ͘ͳ͍
  12. ϦϑΝΫλϦϯάͷํ਑ɾվ • Ұ୴ɺͦΕͬΆཻ͍౓ͷؔ਺ʹ෼ׂ͍ͯ͜͠͏

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

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

  15. ݁Ռ • ී௨ʹ͏·͍͘͘ (ຊ౰ʹී௨Ͱ͢Ͷɾɾɾ) • before: Ͳ͜ͰԿͷॲཧΛ΍͍ͬͯΔ͔͕ύοͱΘ͔Βͳ͍
 => after: ؔ਺ʹ෼ׂ͞Εɺ໊લ͕෇͚ΒΕͯɺΘ͔Γ΍͘͢ͳͬͨ

    • before: ϧʔϓ͕ଟॏʹͳ͍ͬͯͯม਺ͷείʔϓ͕෼͔Γʹ͍͘
 => after: ֤ϧʔϓ಺ͷॲཧ͕ؔ਺ʹͳͬͨͷͰɺείʔϓ͸Θ͔Γ΍͘͢ͳͬͨ • ෼ׂͨ͠୯ҐͰ୯ମςετ΋ॻ͚ΔΑ͏ʹ
  16. ϙΠϯτ • ୯ʹಡΈ΍͘͢ͳͬͨ & 
 มߋ͍ͨ͠ͱ͖ʹมߋ͢΂͖Օॴ͕Θ͔Γ΍͘͢ͳ͚ͬͨͩ • ΞʔΩςΫνϟʹຊ࣭తͳมԽ͸ͳ͍ • ͕ɺ໨ઌͷ՝୊͸ղܾͨ͠ͱ͍͏࿩

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

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

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

  20. ϦϑΝΫλϦϯάଥڠϧʔτ (τοϓμ΢ϯܕ) • Lv.0 Ϣʔεέʔε͕΂ͬͨΓ௕ͬͨΒ͘͠ίʔυͰදݱ͞Ε͍ͯΔ
 (యܕతͳτϥϯβΫγϣϯεΫϦϓτ) • Lv.1 ؔ਺܈ʹ෼ղ͢Δ (Composed

    Method ύλʔϯ) • Lv.2 ؔ਺܈Λ public ʹͯ͠ɺͦΕͧΕςετΛॻ͘
 (Ծশ: Well-organized Transaction Script) • Lv.3 ֤ؔ਺܈ʹυϝΠϯϞσϧͱͯ͠ͷৼΔ෣͍Λ࣋ͨͤΔ
  21. Lv.0

  22. Lv.1

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

  24. Well-organized Transaction Script • ͋͘·ͰϢʔεέʔεΛ෼ղ͍ͯ͠Δ͚ͩͰɺ
 ຊ࣭తͳมԽ͸ى͖͍ͯͳ͍ (υϝΠϯϞσϧͰ͸ͳ͍) • ؔ਺ཻ͕౓Λߟ͑ͯ෼ղ͞Εɺ୯ମςετՄೳʹͳ͍ͬͯΔɻ
 ֤ؔ਺͸ɺࠓޙυϝΠϯϞσϧʹͳΔީิʹͳ͍ͬͯΔ


    (Lv.3΁ͷมԽ)
  25. ࢲݟ: Lv.1 ͰࢭΊͪΌ͏ਓଟ͍ • ʮ͜ͷϝιου͸͜ͷϢʔεέʔε͔Β͔͠࢖ΘΕͳ͍͔Β private ϝιουʹ͠·͢ʯ=> ؒҧͬͯ͸ͳ͍͕ • 1ͭͷϢʔεέʔε͔Β͔͠࢖ΘΕͳͯ͘΋

    public Ͱྑ͍ • public ʹ͢Δͱಉ࣌ʹɺྑ͍ΠϯλʔϑΣΠεΛҙࣝ͢Δɻ
 ݁Ռςετ΋͠΍͘͢ͳΔɻ
  26. ͍ͭଥڠϧʔτΛબͿͷ͔ • υϝΠϯϞσϧΛߟ࡯͢Δҙຯ͕ബ͍ͱ͖ • υϝΠϯϞσϧΛຊ֨తʹߟ࡯͢Δೳྗ·ͨ͸͕࣌ؒͳ͍ͱ͖ • ͦΕͰ΋ஈ֊తʹલʹਐΉ͜ͱ͕Ͱ͖Δ

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

  28. ·ͱΊ

  29. ຊൃදͷ·ͱΊ • τϥϯβΫγϣϯεΫϦϓτΛϦϑΝΫλϦϯά͢Δͱ͖ɺ
 ࠷ॳ͔ΒυϝΠϯϞσϧΛߟ࡯͢Δख๏ (Ԧಓϧʔτ) ͱ
 Ұ୴ϢʔεέʔεΛ෼ղ͍ͯ͘͠ख๏ (ଥڠϧʔτ) ͕͋Δ •

    ଥڠϧʔτ͔ΒͰ΋ɺ࠷ऴతʹυϝΠϯϞσϧ͕ߟ࡯͞Εͨঢ়ଶ ʹొΔ͜ͱ͕Ͱ͖Δ • ͦ΋ͦ΋υϝΠϯΛߟ࡯͢Δҙຯ͕ബ͍ͱ͖΋͋ΔΑ
  30. എܠʹڵຯ΋ͬͯԼͬͨ͞ํ΁ • ͜ͷ୊ࡐͱͳͬͨτϥϯβΫγϣϯεΫϦϓτ
 ͕ੜ·ΕͨഎܠͳͲ͸
 jsconfjp 2021 Ͱొஃͨ͠ࢿྉ (ӈ) Λ
 ͥͻ͝ࢀর͍ͩ͘͞

    • ελσΟαϓϦͰ͸ɺڭҭ/ֶशυϝΠϯʹڵຯͷ͋Δ
 ΤϯδχΞΛืू͍ͯ͠·͢
 (ڵຯ͋Δํ Twitter @qsona ʹϝϯγϣϯ΍DM͍ͩ͘͞)
  31. కΊͷҰ۟ • P of EAA 2002೥ Զͨͪੜ͖ͯΔ 2022೥