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

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

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

第 2 回: 命名

---

セッションリスト

第 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

Other Decks in Programming

Transcript

  1. ߨٛͷߏ੒ - ಋೖͱݪଇ - ࣗવݴޠ: ໋໊, ίϝϯτ - ܕͷߏ଄: ঢ়ଶ,

    ؔ਺ - ܕؒͷߏ଄: ґଘؔ܎I, ґଘؔ܎II - ϨϏϡʔ ໋໊ > ಋೖ
  2. ໋໊ͷର৅ - Ϋϥε, ΠϯλʔϑΣΠε, ྻڍܕ, ߏ଄ମ, ϓϩτίϧ, τϨΠτ - ม਺,

    ϓϩύςΟ, ϑΟʔϧυ, ύϥϝʔλ, ఆ਺ - ؔ਺, ϝιου, खଓ͖, αϒϧʔνϯ - είʔϓ, ύοέʔδ, Ϟδϡʔϧ, ໊લۭؒ - Ϧιʔε, ϑΝΠϧ, σΟϨΫτϦ, ID - etc. ໋໊ > ಋೖ
  3. Α໊͍લͱ͸ ਖ਼֬ - isVisible ΛʮԻʯʹ࢖ͬͯ͸͍͚ͳ͍ આ໌త - w / h

    ΑΓ΋ width / height - image ΑΓ΋ imageView / imageBitmap / imageUrl ໋໊ > ಋೖ
  4. จ๏͕ॏཁͳཧ༝ ໰୊ 1: CallbackEventMessageClickViewText ͸ԿΛҙຯ͢Δʁ ղ౴ 1ʁ: A text of

    a click view (?) of callback event message (???) ໰୊ 2: MessageTextViewClickEventCallback ͸ԿΛҙຯ͢Δʁ ղ౴ 2: A callback of click events on a message text view ໋໊ > ਖ਼͍͠จ๏Λ࢖͏
  5. ໊લͷछྨ 2/2 - ܗ༰ࢺɾ෼ࢺ: ΠϯλʔϑΣΠεɾঢ়ଶΛࣔ͢ܕ΍஋ Iterable , PLAYING , CONNECTED

    - ٙ໰ܗɾࡾਓশͷ(ॿ)ಈࢺ: ਅِ஋ɾਅِ஋Λฦؔ͢਺ isTextVisible , contains , equalsTo , may / shouldShow - લஔࢺΛ࣋ͭ෭ࢺ: ܕม׵Λ͢Δؔ਺, ίʔϧόοΫؔ਺ toInt , fromMemberId , onClickEvent ໋໊ > ਖ਼͍͠จ๏Λ࢖͏
  6. จ๏: ໊ࢺ ॏཁͳ୯ޠΛ࠷ޙʹஔ͘ (= ࠷ޙͷ୯ޠͰͦΕ͕ԿͰ͋Δ͔ɾԿΛ͢Δ͔Λࣔ͢) ! : MessageEventHandler , buttonHeight

    ! : xyzHeightForPortrait (Ϋϥε໊ͱͯ͠͸ෆద) ! : HandlerMessageEvent (ϋϯυϥͷ৔߹), heightPortrait ྫ֎: ʮঢ়ଶ΍ଐੑΛࣔؔ͢਺໊ʯ͸લஔࢺͰऴΘΔ৔߹͕͋Δ e.g., indexOf(value) , maxValueIn(array) ໋໊ > ਖ਼͍͠จ๏Λ࢖͏
  7. จ๏: ໋ྩܗ ಈࢺΛ࠷ॳʹஔ͘ "X Λऔಘ͢Δ": ! getX, " xGet "Y

    Λ౤ߘ͢Δ": ! postY, " yPost "Z ʹରԠͤ͞Δ": ! mapToZ, " toZMap, zToMap ໋໊ > ਖ਼͍͠จ๏Λ࢖͏
  8. ؒҧͬͨޠॱ͕࢖ΘΕΔཧ༝ class UserActionEvent class UserActionEventSwipe: UserActionEvent() class UserActionEventClickMessageText: UserActionEvent() class

    UserActionEventClickProfileImage: UserActionEvent() mutableListOf<UserActionEventClickProfileImage>() ίʔυΛʮ࢖͏ଆʯͰͲ͏ݟ͑Δ͔͕ॏཁ ݟͨ໨ͷ౷Ұײ ͷΈʹয఺Λ౰ͯͯ͸ͳΒͳ͍ ໋໊ > ਖ਼͍͠จ๏Λ࢖͏
  9. ࠓճͷ಺༰ - ਖ਼͍͠จ๏Λ࢖͏ - ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ - ҙຯͷڱ͍ޠΛબͿ - ࠞཚΛটུ͘ޠΛආ͚Δ -

    ܕ΍୯ҐΛࣔ͢୯ޠΛՃ͑Δ - ߠఆతͳදݱΛ࢖͏ ໋໊ > ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ
  10. ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ දݱ͢Δ΂͖಺༰ ! ͦͷม਺΍Ϋϥε͕ Կ Ͱ͋Δ͔ ! ͦͷؔ਺͕ Կ Λ͢Δ͔

    ݴٴ͢Δ΂͖Ͱͳ͍಺༰ ! ͦͷίʔυΛ ͩΕ ͕࢖͏͔ ! ͦͷίʔυ͕ ͍ͭɾͲ͜ͰɾͳͥɾͲͷΑ͏ʹ ࢖ΘΕΔ͔ ໋໊ > ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ
  11. ؔ਺໊ͷྫ: એݴଆ ! ͦͷؔ਺͕ Կ Λ͢Δ͔Λදݱ͢Δ class MessageRepository { fun

    storeReceivedMessage(data: MessageData) { ! ͦͷؔ਺͕ ͍ͭ ݺ͹ΕΔ͔Λදݱ͢Δ class MessageRepository { fun onMessageReceived(data: MessageData) { ໋໊ > ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ
  12. ؔ਺໊ͷྫ: ʮ͍ͭʯ͕Α͘ͳ͍ผͷཧ༝ ؔ਺ͷ੹೚͕ᐆດʹͳΔ class MessageViewPresenter { fun onMessageReceived(data: MessageData) {

    // View presentation code for new message ... repository.onMessageReceived(data) // !! ॏෳݺͼग़͠ͷόάͷݪҼʹͳΔ͜ͱ΋ repository.onMessageReceived(messageData) presenter.onMessageReceived(messageData) ໋໊ > ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ
  13. Ҿ਺໊ͷྫ: એݴଆ ! true ͷͱ͖ Կ͕ى͜Δ͔͕Θ͔Δ fun showHistory(shouldShowDialogOnError: Boolean) !

    true ͷͱ͖ Կ͕ى͜Δ͔Θ͔Βͳ͍ fun showHistory(isCalledAtMainScreen: Boolean) ໋໊ > ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ
  14. Ҿ਺໊ͷྫ: ʮͩΕʯ͕Α͘ͳ͍ཧ༝ 1: ໊લ͕ ࠷৽ͷঢ়گ ͱҰக͠ͳ͘ͳΔ // ... from another

    screen requiring dialog showHistory(isCalledAtMainScreen = true) 2: Ҿ਺͕ ଞͷ໨త ͷͨΊʹ࢖ΘΕΔ if (isCalledAtMainScreen) { setScreenTitle("Main") ... 1 ͱ 2 ͕ಉ࣌ʹى͜Δͱ όά͕ൃੜ͢Δ ໋໊ > ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ
  15. ୯ޠͷҙຯͷ޿͞: ྫ 1/2 ໰୊: initializationFlag ͸ԿΛҙຯ͢Δʁ - shouldInitialize - isInitializing

    - is/wasInitialized - isInitializable - isNotInitialized (!!) ʢ࠷ޙҎ֎ͷʣ্هͷͲΕ͔Ͱஔ͖׵͑Δ ໋໊ > ҙຯͷڱ͍ޠΛબͿ
  16. ୯ޠͷҙຯͷ޿͞: ྫ 2/2 ໰୊: sizeLimit ͸ԿΛҙຯ͢Δʁ - max, minʁ -

    height, width, byte, characters, lengthʁ maxHeight ౳ͱ໋໊͢Δ ໋໊ > ҙຯͷڱ͍ޠΛબͿ
  17. ஔ͖׵͑Δ΂͖୯ޠͷྫ - flag: is, was, should, can, may, will ...

    - check: is, query, verify, measure, filter, notify, update ... - good, fine: valid, completed, reliable, secure, satisfies ... - old: previous, stored, expired, invalidated, deprecated ... - tmp, retval: ࣮ࡍͷ໊લ ࣙॻ΍ྨޠࣙయΛ࢖͏ ໋໊ > ҙຯͷڱ͍ޠΛબͿ
  18. ҙຯͷڱ͍ޠΛબͿ: ·ͱΊ - flag ΍ check ౳ͷ ҙຯͷ޿͍୯ޠ Λஔ͖׵͑Δ -

    ࣙॻ ΍ ྨޠࣙయ Λ࢖͏ ໋໊ > ҙຯͷڱ͍ޠΛબͿ
  19. ࠓճͷ಺༰ - ਖ਼͍͠จ๏Λ࢖͏ - ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ - ҙຯͷڱ͍ޠΛબͿ - ࠞཚΛটུ͘ޠΛආ͚Δ -

    ܕ΍୯ҐΛࣔ͢୯ޠΛՃ͑Δ - ߠఆతͳදݱΛ࢖͏ ໋໊ > ࠞཚΛটུ͘ޠΛආ͚Δ
  20. ུޠ: ྫ 1/2 ໰୊: im ͱ͍͏໊લ͸ԿΛҙຯ͢Δʁ input method, illegal message,

    instance manager ... ಠࣗͷུޠΛආ͚Δ ʮೝࣝʯ͸ʮ૝ىʯΑΓ΋؆୯3 3 100 Things: Every Designer Needs to Know About People, Susan Weinschenk, 2011 ໋໊ > ࠞཚΛটུ͘ޠΛආ͚Δ
  21. ུޠ: ྫ 2/2 ໰୊: str ͱ͍͏໊લ͸ԿΛҙຯ͢Δ? string, structure, stream, streak,

    street, sorted transaction record Ұൠతͳུޠ͸ڐ༰Ͱ͖Δ͜ͱ΋ - URL ΍ TCP ౳ͷ࢖༻͸໰୊ͳ͍ - string Λҙຯ͢Δ str ͸ ݶΒΕͨείʔϓ ͳΒڐ༰͞ΕΔ͔΋ ໋໊ > ࠞཚΛটུ͘ޠΛආ͚Δ
  22. ࠓճͷ಺༰ - ਖ਼͍͠จ๏Λ࢖͏ - ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ - ҙຯͷڱ͍ޠΛબͿ - ࠞཚΛটུ͘ޠΛආ͚Δ -

    ܕ΍୯ҐΛࣔ͢୯ޠΛՃ͑Δ - ߠఆతͳදݱΛ࢖͏ ໋໊ > ܕ΍୯ҐΛࣔ͢୯ޠΛՃ͑Δ
  23. ܕ΍୯ҐΛࣔ͢୯ޠΛՃ͑Δ ʮ੔਺ܕʯͰ͸୯Ґ΍࣮ମ͕ᐆດͳ͜ͱ͕͋Δ - timeout: timeoutInMillis, timeoutInSeconds - width: widthInPixels, widthInPoints,

    widthInInches - color: argbColorInt, colorResId - i, j, k: userIndex, row, col ໋໊ > ܕ΍୯ҐΛࣔ͢୯ޠΛՃ͑Δ
  24. ܕ΍୯ҐΛࣔ͢୯ޠΛՃ͑Δ: ༨ஊ ΫϥεͰ୯ҐΛදݱ͢Δ͜ͱ΋Մೳ class Inch(val value: Int) class Centimeter(val value:

    Int) fun setWidth(width: Inch) = ... setWidth(Centimeter(10)) // Compile error! - Scala ͷ஋Ϋϥε΍ Kotlin ͷΠϯϥΠϯΫϥε౳΋༗ޮ - Kotlin ͷ Duration ͷΑ͏ʹ୯ҐΛಁաతʹѻ͏͜ͱ΋Ͱ͖Δ ໋໊ > ܕ΍୯ҐΛࣔ͢୯ޠΛՃ͑Δ
  25. ࠓճͷ಺༰ - ਖ਼͍͠จ๏Λ࢖͏ - ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ - ҙຯͷڱ͍ޠΛબͿ - ࠞཚΛটུ͘ޠΛආ͚Δ -

    ܕ΍୯ҐΛࣔ͢୯ޠΛՃ͑Δ - ߠఆతͳදݱΛ࢖͏ ໋໊ > ߠఆతͳදݱΛ࢖͏
  26. ߠఆతͳදݱΛ࢖͏ ! : ߠఆతͳޠ, isEnabled ! : ൱ఆతͳޠ, isDisabled !

    : ߠఆతͳޠ + notɾnoɾnon, isNotEnabled ! : ൱ఆతͳޠ + notɾnoɾnon, isNotDisabled isNotDisabled → isEnabled isNotEnabled → isDisabled ͸ ୯७ʹஔ׵Մೳ ໋໊ > ߠఆతͳදݱΛ࢖͏
  27. ࠓճͷ಺༰ - ਖ਼͍͠จ๏Λ࢖͏ - ʮ͍ͭɾͩΕ͕ʯΑΓʮԿʯΛઆ໌͢Δ - ҙຯͷڱ͍ޠΛબͿ - ࠞཚΛটུ͘ޠΛආ͚Δ -

    ܕ΍୯ҐΛࣔ͢୯ޠΛՃ͑Δ - ߠఆతͳදݱΛ࢖͏ ໋໊ > ݴޠɾϓϥοτϑΥʔϜɾϓϩμΫτͷن໿ʹै͏
  28. ݴޠɾϓϥοτϑΥʔϜɾϓϩμΫτͷن໿ʹै͏ ݴޠɾϓϥοτϑΥʔϜݻ༗ͷ ن໿ɾϧʔϧɾෆจ཯ ʹै͏ - จ๏: itemCount vs. numItem -

    ུޠ: milliSeconds vs. millis vs. ms ඪ४ϥΠϒϥϦɾAPI Λࢀর͢Δ ෆ໌ͳ఺ʹ͍ͭͯ͸ϓϩμΫτͰن໿ΛఆΊΔ ໋໊ > ݴޠɾϓϥοτϑΥʔϜɾϓϩμΫτͷن໿ʹै͏