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

読みやすいコードの書き方 第 7 回 / Code readability: Session ...

340

読みやすいコードの書き方 第 7 回 / Code readability: Session 7 (ver. 2, Ja)

第 7 回: 依存関係 II

---

セッションリスト

第 1 回: 導入と原則
- https://speakerdeck.com/munetoshi/code-readability-session-1-ver-2-ja

第 2 回: 命名
- https://speakerdeck.com/munetoshi/code-readability-session-2-ver-2-ja

第 3 回: コメント
- https://speakerdeck.com/munetoshi/code-readability-session-3-ver-2-ja

第 4 回: 状態
- https://speakerdeck.com/munetoshi/code-readability-session-4-ver-2-ja

第 5 回: 関数
- https://speakerdeck.com/munetoshi/code-readability-session-5-ver-2-ja

第 6 回: 依存関係 I
- https://speakerdeck.com/munetoshi/code-readability-session-6-ver-2-ja

第 7 回: 依存関係 II
- https://speakerdeck.com/munetoshi/code-readability-session-7-ver-2-ja

第 8 回: レビュー
- https://speakerdeck.com/munetoshi/code-readability-session-8-ver-2-ja

---

関連書籍 「読みやすいコードのガイドライン - 持続可能なソフトウェア開発のために」
- https://gihyo.jp/book/2022/978-4-297-13036-7

英語版のリスト: https://gist.github.com/munetoshi/65a1b563fb2c271f328c121a4ac63571#file-code-readability-links-md

以前のバージョン (英語): https://speakerdeck.com/munetoshi/code-readability

© 2019-2023 Munetoshi Ishikawa, supported by LINE corporation

Munetoshi Ishikawa

May 11, 2023
Tweet

More Decks by Munetoshi Ishikawa

Transcript

  1. લճͷ͓͞Β͍: ݁߹౓ ऑ͍݁߹Λ࢖͏ - ελϯϓ݁߹ɾσʔλ݁߹Λҙࣝͯ͠࢖͏ ஫ҙ͢Δ΂͖݁߹ - ಺༰݁߹: ಺෦ͷৄࡉͳಈ࡞ʹґଘ͢Δؔ܎ -

    ڞ௨݁߹ɾ֎෦݁߹: Մมͳάϩʔόϧσʔλ࢖͏ؔ܎ - ੍ޚ݁߹: ʮԿΛ͢Δ͔ʯΛҾ਺ͰܾΊΔؔ܎
  2. ߨٛͷߏ੒ - ಋೖͱݪଇ - ࣗવݴޠ: ໋໊, ίϝϯτ - ܕͷߏ଄: ঢ়ଶ,

    ؔ਺ - ܕؒͷߏ଄: ґଘؔ܎I, ґଘؔ܎II - ϨϏϡʔ ґଘؔ܎
  3. ґଘؔ܎ IɾII ͷ಺༰ ґଘؔ܎ I: - ݁߹౓ ґଘؔ܎ II: -

    ํ޲ - ॏෳ - ໌ࣔੑ ґଘؔ܎ > ํ޲
  4. ޷·͍͠ґଘͷํ޲ - ݺͼग़͠ݩ → ݺͼग़͠ઌ - ৄࡉ → ந৅ -

    ෳࡶ → ୯७ - Մม → ෆม - ΞϧΰϦζϜ → σʔλߏ଄ - සൟʹߋ৽͞ΕΔϨΠϠ → ҆ఆతͳϨΠϠ ґଘؔ܎ > ํ޲
  5. ޷·͍͠ґଘͷํ޲ - ݺͼग़͠ݩ → ݺͼग़͠ઌ - ৄࡉ → ந৅ -

    ෳࡶ → ୯७ ґଘؔ܎ > ํ޲ > ݺͼग़͠ݩ → ݺͼग़͠ઌ
  6. ݺͼग़͠ݩ΁ͷґଘ: ʮΑ͘ͳ͍ʯྫ 1/2 class MediaViewPresenter { fun getVideoUri(): Uri =

    ... fun playVideo() { videoPlayerView.play(this) ... class VideoPlayerView { fun play(presenter: MediaViewPresenter) { val uri = presenter.getVideoUri() ґଘؔ܎ > ํ޲ > ݺͼग़͠ݩ → ݺͼग़͠ઌ
  7. Ҿ਺ͱͯ͠஋Λ౉͢ class MediaViewPresenter { fun getVideoUri(): Uri = ... fun

    playVideo() { videoPlayerView.play(this) ... class VideoPlayerView { fun play(presenter: MediaViewPresenter) { val uri = presenter.getVideoUri() ґଘؔ܎ > ํ޲ > ݺͼग़͠ݩ → ݺͼग़͠ઌ
  8. Ҿ਺ͱͯ͠஋Λ౉͢ class MediaViewPresenter { fun getVideoUri(): Uri = ... fun

    playVideo() { videoPlayerView.play(getVideoUri()) ... class VideoPlayerView { fun play(videoUri: Uri) { ... ґଘؔ܎ > ํ޲ > ݺͼग़͠ݩ → ݺͼग़͠ઌ
  9. ޷·͍͠ґଘͷํ޲ - ݺͼग़͠ݩ → ݺͼग़͠ઌ - ৄࡉ → ந৅ -

    ෳࡶ → ୯७ ґଘؔ܎ > ํ޲ > ৄࡉ → ந৅
  10. ৄࡉ΁ͷґଘ: ʮΑ͘ͳ͍ʯྫ open class IntList { fun addElement(value: Int) {

    if (this is ArrayIntList) { ... } else { ... ... class ArrayIntList: IntList() { ... ґଘؔ܎ > ํ޲ > ৄࡉ → ந৅
  11. ৄࡉ΁ͷґଘ: मਖ਼ྫ ΦʔόʔϥΠυΛ࢖ͬͯμ΢ϯΩϟετΛආ͚Δ open class IntList { open fun addElement(value:

    Int) { ... class ArrayIntList: IntList() { override fun addElement(value: Int) { ... ґଘؔ܎ > ํ޲ > ৄࡉ → ந৅
  12. ޷·͍͠ґଘͷํ޲ - ݺͼग़͠ݩ → ݺͼग़͠ઌ - ৄࡉ → ந৅ -

    ෳࡶ → ୯७ ґଘؔ܎ > ํ޲ > ෳࡶ → ୯७
  13. ෳࡶͳίʔυ΁ͷґଘ: ʮΑ͘ͳ͍ʯྫ class UserData( val userId: UserId, ... ) class

    UserDataRequester { fun obtainFromServer(): UserData { ... ґଘؔ܎ > ํ޲ > ෳࡶ → ୯७
  14. ෳࡶͳίʔυ΁ͷґଘ: ʮΑ͘ͳ͍ʯྫ class UserData( val userId: UserId, ... val requester:

    UserDataRequester ) class UserDataRequester { fun obtainFromServer(): UserData { ... ґଘؔ܎ > ํ޲ > ෳࡶ → ୯७
  15. ෳࡶͳίʔυ΁ͷґଘ: Կ͕ʮΑ͘ͳ͍ʯ͔ ෳࡶͳίʔυͷείʔϓɾϥΠϑαΠΫϧɾࢀর͕؅ཧ͕ࠔ೉ - ୯७ͳίʔυ: είʔϓ΍ϥΠϑαΠΫϧ͕ ଟ༷ - ෳࡶͳίʔυ: είʔϓ΍ϥΠϑαΠΫϧͷ

    ݫີͳ؅ཧ͕ඞཁ ؤ݈ੑΛଛͳ͏ - ϦιʔεͷϦʔΫͷݪҼʹͳΓ͏Δ - ୯७ͳίʔυͷ҉໧తͳલఏ৚͕݅૿͑Δ ґଘؔ܎ > ํ޲ > ෳࡶ → ୯७
  16. ґଘͷํ޲: ·ͱΊ ґଘ͸Ұํ޲ʹ͢Δ - ݺͼग़͠ݩ → ݺͼग़͠ઌ - ෳࡶɾৄࡉɾՄม →

    ୯७ɾந৅ɾෆม ྫ֎: ඇಉظݺͼग़͠ɾϝσΟΤʔλύλʔϯ౳ ྫ֎Λ࢖͏৔߹Ͱ΋ ໨తɾείʔϓɾϥΠϑαΠΫϧΛ੍ݶ͢Δ ґଘؔ܎ > ํ޲ > ·ͱΊ
  17. ґଘؔ܎ IɾII ͷ಺༰ ґଘؔ܎ I: - ݁߹౓ ґଘؔ܎ II: -

    ํ޲ - ॏෳ - ໌ࣔੑ ґଘؔ܎ > ॏෳ
  18. ਺चͭͳ͗ͷґଘ: ʮΑ͘ͳ͍ʯྫ class TimestampPresenter { val dataProvider: MessageDataProvider = ...

    fun invalidateViews() { // Update timestamp by `dataProvider` ... class MessageTextPresenter { val timestampPresenter: TimestampPresenter = ... fun invalidateViews() { val messageData = timestampPresenter.dataProvider... // Update message text view by `messageData` ґଘؔ܎ > ॏෳ > ਺चͭͳ͗ͷґଘ
  19. ਺चͭͳ͗ͷґଘ: मਖ਼ྫ class TimestampPresenter { val dataProvider: MessageDataProvider = ...

    fun invalidateViews() { // Update timestamp by `dataProvider` ... class MessageTextPresenter { val dataProvider: MessageDataProvider = ... fun invalidateViews() { // Update message text view by `dataProvider` ґଘؔ܎ > ॏෳ > ਺चͭͳ͗ͷґଘ
  20. ґଘؔ܎ IɾII ͷ಺༰ ґଘؔ܎ I: - ݁߹౓ ґଘؔ܎ II: -

    ํ޲ - ॏෳ - ໌ࣔੑ ґଘؔ܎ > ໌ࣔੑ
  21. ҉໧తͳґଘؔ܎ 1/3 Ϋϥεͷఆ͚ٛͩͰ͸ൃݟͰ͖ͳ͍ґଘؔ܎ interface Interface class Implementation : Interface class

    Caller(val interface: Interface) Caller ͸ Implementation ͷมߋͷӨڹΛड͚͏Δ Caller(Implementation()) ґଘؔ܎ > ໌ࣔੑ
  22. ա౓ͳந৅Խ: ʮΑ͘ͳ͍ʯྫ interface StringProvider { fun queryString(id: Int): String }

    class ProfileDataRepository : StringProvider { /** Returns user name of the given [userId] */ override fun queryString(userId: Int): String = ... } class ProfilePresenter(val nameProvider: StringProvider, ...) { fun updateProfileView(userId: Int) { userNameView.text = nameProvider.queryString(userId) ... ґଘؔ܎ > ໌ࣔੑ > ա౓ͳந৅Խ
  23. ա౓ͳந৅Խ: मਖ਼ྫ class ProfileDataRepository { fun queryUserName(userId: Int): String =

    ... } class ProfilePresenter(val repository: ProfileDataRepository, ...) { fun updateProfileView(userId: Int) { userNameView.text = repository.queryUserName(userId) ... ґଘؔ܎ > ໌ࣔੑ > ա౓ͳந৅Խ
  24. ա౓ͳந৅Խ: ஫ҙ͢Δ΂͖఺ 1/2 ܧঝΛ࢖͏໨తΛ໌֬ʹ͢Δ - ଟ૬ੑͷར༻ - ௚࿨ܕͷར༻: sealed class

    - ґଘੑٯస΍࣮૷ͷ෼཭: ϞδϡʔϧԽɾϥΠϒϥϦԽ ґଘؔ܎ > ໌ࣔੑ > ա౓ͳந৅Խ
  25. ҉໧తͳҾ਺ͷมҬ: ʮΑ͘ͳ͍ʯྫ ໰୊: ҎԼͷίʔυͷʮΑ͘ͳ͍ʯ఺͸ʁ fun setViewBackgroundColor(colorString: String) { val colorCode

    = when(colorString) { "red" -> 0xFF0000 "green" -> 0x00FF00 else -> 0x000000 } view.setBackgroundColor(colorCode) } ґଘؔ܎ > ໌ࣔੑ > ҉໧తͳมҬ
  26. ҉໧తͳҾ਺ͷมҬ: मਖ਼ํ๏ มҬΛࣔͨ͢ΊͷϞσϧΛ࡞Δ enum class BackgroundColor(val colorCode: Int) { RED(0xFF0000),

    GREEN(0x00FF00) } fun setViewBackgroundColor(color: BackgroundColor) { view.setBackgroundColor(color.colorCode) } ґଘؔ܎ > ໌ࣔੑ > ҉໧తͳมҬ