0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

今さらリーダブルコード

Posted at

今さらながら、改めてリーダブルコードを読んだ。

特に改めて意識したいと思ったこと

  • 変数名
    image.png

  • 制御フロー
    image.png

  • 無関係の下位問題の抽出
    image.png

  • きれいなインターフェース
    image.png

  • コードをより明確にする
    image.png

実践

少しだが、自分のアプリのコード(Flutter)を直してみた。

無関係の下位問題の抽出

  • 一箇所目:リストの中の特定の項目の入れ替えは、アイテムの更新とは無関係な下位問題なので、別メソッドに切り出した。
    AndroidStudioでは右クリックでrefactor > extractでできるので楽ちん。
    before
Future<void> updateItem(ListItemModel item) async {
    state = state.copyWith(
      items: [
        for (int i = 0; i < state.items!.length; i++)
          if (i == state.items!.indexOf(item)) item else state.items![i],
      ],
    );
    await state.update(item);
  }

after

///アイテムの更新
  Future<void> updateItem(ListItemModel item) async {
    updateStateOfItem(item);
    await state.update(item);
  }

  ///アイテムの状態を更新
  void updateStateOfItem(ListItemModel item) {
    state = state.copyWith(
      items: [
        for (int i = 0; i < state.items!.length; i++)
          if (i == state.items!.indexOf(item)) item else state.items![i],
      ],
    );
  }

  • 2箇所目
    DBへ接続している部分と、リストの並べ替えを別メソッドにした。

before

///買い物リストを取得して返す
  Future<List<ListItemModel>> findShopList(String? listId) async {
    ///db接続設定
    FirebaseFirestore db = FirebaseFirestore.instance;
    final shoppingRef = db.collection(EnvSettings.xxx);

    List<ListItemModel> result = [];
    final QuerySnapshot snapshot =
        await shoppingRef.where('xxx', isEqualTo: xxx).get();

    final List<ListItemModel> items =
        snapshot.docs.map((DocumentSnapshot document) {
      ///jsonからオブジェクトに変換
      Map<String, dynamic> data = document.data() as Map<String, dynamic>;
      ListItemModel result =
          ListItemModel.fromJson(docId: document.id, json: data);
      return result;
    }).toList();

    ///お店・カテゴリ・名前順に並び替え
    if (AppUtils.isNotEmptyArray(items)) {
      items.sort((a, b) {
        int shopResult =
            Enum.compareByIndex(a.shop.getShop(), b.shop.getShop());
        if (shopResult != 0) return shopResult;
        int catResult = Enum.compareByIndex(a.category, b.category);
        if (catResult != 0) return catResult;
        return a.itemName.compareTo(b.itemName);
      });
    }
    result = items;
    return result;
  }

after
何をしているかぱっと見でわかりやすくなった、気がする。

/// 買い物リストを取得して返す
Future<List<ListItemModel>> findShopList(String? listId) async {
  /// DBから買い物リストを取得
  ShoppingListRepository shoppingListRepository = ShoppingListRepository();
  List<ListItemModel> items =
      await shoppingListRepository.getItemList(listId: listId);

  /// お店・カテゴリ・名前順に並び替え
  List<ListItemModel> sortedItems = sortByShopCategoryName(items);

  return sortedItems;
}

おわり

改めて読むことで、以前よりも当たり前と思えていることが増えていてよかった。一方、上に記載したような、改めて意識したいと思えることもあったので今後に活かしたい。
それと、久しぶりにFlutterを触ったらやはり開発体験がよいと思った。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?