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

オブジェクト指向で類型化するコードレビュー

akkie76
June 29, 2022
340

 オブジェクト指向で類型化するコードレビュー

akkie76

June 29, 2022
Tweet

Transcript

  1. 自己紹介 Akihiko Sato / 株式会社ラクス Lead Engineer SaaS 開発 (Backend,

    Frontend) / Mobile 開発 (iOS, Android) 上流工程、コードレビュー、チームの課題改善など 読書 / コーヒー / HHKB / 縄跳び
  2. 観点について ① 指摘の観点を 7 つに分類 1. Design (設計) 2. Simplicity

    (理解容易性) 3. Naming (クラス、メソッド、変数名などの命名) 4. Style (コードスタイル)
  3. 観点について ② 指摘の観点を 7 つに分類 5. Functionality (機能を充足しているか) 6. Tests

    (自動テストが適切である) 7. Document (コメント、ドキュメンに関連)
  4. 観点について ③ オブジェクト指向の観点で重要な項目 ・ Design (設計) ・ Simplicity (理解容易性) ・

    Naming (クラス、メソッド、変数名などの命名) ・ Style (コードスタイル)
  5. Design (設計) class SummerDiscountManager { lateinit var manager: DiscountManager /**

    * 商品を追加する */ fun add(product: Product): Boolean { if (product.id < 0) { // バリデーション ① throw IllegalArgumentException() } if (product.name.isEmpty()) { // バリデーション ② throw IllegalArgumentException() } val temp: Int = if (product.canDiscount) { // 条件分岐 ① discountMmanageranager.totalPrice + manager.getDiscountPrice(product.price) } else { manager.totalPrice + product.price } return if (temp < 3000) { // 条件分岐 ② manager.totalPrice = temp manager.discountProducts.add(product) true } else { false } } } 「良いコード / 悪いコードで学ぶ設計入門」より
  6. Simplicity (理解容易性) 分岐が多い if 分例 val hitPointRate: float = member.hitPoint

    / menber.maxHitPoint var currentHealthCondition: HealthCondition = HealthCondition.NONE if (hitPointRate == 0) { currentHealthCondition = HealthCondition.DEAD } else if (hitPointRate < 0.3) { currentHealthCondition = HealthCondition.DANGER } else if (hitPointRate < 0.5) { currentHealthCondition = HealthCondition.CAUTION } else { currentHealthCondition = HealthCondition.FINE } return currentHealthCondition 「良いコード / 悪いコードで学ぶ設計入門」より
  7. Simplicity (理解容易性) コメントでの指摘例 val hitPointRate: float = member.hitPoint / menber.maxHitPoint

    var currentHealthCondition: HealthCondition = HealthCondition.NONE if (hitPointRate == 0) { return currentHealthCondition = HealthCondition.DEAD } if (hitPointRate < 0.3) { return currentHealthCondition = HealthCondition.DANGER } if (hitPointRate < 0.5) { return currentHealthCondition = HealthCondition.CAUTION } return currentHealthCondition = HealthCondition.FINE
  8. 具体的な利用方法 指摘例 MUST ( Design ):ドメインロジックが Controller クラスに実装されてます。 domain 層の対象

    package に新しくクラスを作成して実装を移してください。 コメントの prefix として利用する。
  9. [ 発展編 ] prefix を付けたコメントを集計 GitLab API ( Note API

    )を利用してコメントを集計します。 具体的な方法は、 ラクス Advent Calendar 2021 ( 12/23 ) 「 GitLab API で Merge Request のコメントを一括取得する方法」 をご覧ください。