TOP

このエントリーをはてなブックマークに追加

リファクタリング技法

ここではCode Smellを解消するために参考となるリファクタリング技法や、その他参考になりそうな概念を紹介していこうと思います。

リファクタリングに関してはこの辺りの書籍が参考になると思います。


リファクタリング第2版
refactoring
レガシーコード改善ガイド
legacy_code

関連するリファクタリング技法

Composing Methods

Composing Methods

Substitute Algorithm

Substitute Algorithm(アルゴリズムの置き換え) メソッドのシグニチャは変えずに本体となるアルゴリズムを新しいものに置き換えましょう。

String foundPerson(String[] people){
  for (int i = 0; i < people.length; i++) {
    if (people[i].equals("Don")){
      return "Don";
    }
    if (people[i].equals("John")){
      return "John";
    }
    if (people[i].equals("Kent")){
      return "Kent";
    }
  }
  return "";
}
String foundPerson(String[] people) {
  List candidates =
    Arrays.asList(new String[] {"Don", "John", "Kent"});
  for (int i=0; i < people.length; i++) {
    if (candidates.contains(people[i])) {
      return people[i];
    }
  }
  return "";
}

関連するCode Smell