カロリーメイトください

Barbaroi Ware(バルバロイ・ウェア)という名前でアプリ開発してます

『リーダブルコード』「14章 テストと読みやすさ」 学習記録1

リーダブルコード ―より良いコードを書くためのシンプルで実践的なテクニック (Theory in practice)

リーダブルコード ―より良いコードを書くためのシンプルで実践的なテクニック (Theory in practice)

序文

リーダブルコード学習10日目。
珍しく期限のあるタスクが入って、ちょっと更新お休み気味。
またがんばります。

GitHub

github.com

進捗

  • 14章 テストと読みやすさ (学習時間:2時間)

コード実装部分(一部)

  • 簡潔なテストを書く

\CodeExample\14章 テストと読みやすさ\bad-test.js

// 読みにくいテストを改善する
function test1() {
  let docs = new Array(5);
  for (let i = 0; i < docs.length; i++) {
    docs[i] = new ScoredDocument();
  }

  docs[0].url = "http://example.com";
  docs[0].score = -5.0;
  docs[1].url = "http://example.com";
  docs[1].score = 1;
  docs[2].url = "http://example.com";
  docs[2].score = 4;
  docs[3].url = "http://example.com";
  docs[3].score = -9998.7;
  docs[4].url = "http://example.com";
  docs[4].score = 3.0;

  sortAndFilterDocs(docs);

  assert(docs.length === 3);
  assert(docs[0].score === 4);
  assert(docs[0].score === 3.0);
  assert(docs[0].score === 1);
}

/*******************
 改善1
********************/
// 目障りな繰り返しを関数に切り出す
function test1() {
  let docs = new Array();

  addScoredDoc(docs, -5.0, "http://example.com");
  addScoredDoc(docs, 1, "http://example.com");
  addScoredDoc(docs, 4, "http://example.com");
  addScoredDoc(docs, -9998.7, "http://example.com");
  addScoredDoc(docs, 3, "http://example.com");

  sortAndFilterDocs(docs);

  assert(docs.length === 3);
  assert(docs[0].score === 4);
  assert(docs[0].score === 3.0);
  assert(docs[0].score === 1);
}

function addScoredDoc(docs, score, url) {
  docs.push(new ScoredDocument(score, url));
}

/*******************
 改善2
********************/
// 重要性の低いフィールドを省略する
function test1() {
  let docs = new Array();

  addScoredDoc(docs, -5.0);
  addScoredDoc(docs, 1);
  addScoredDoc(docs, 4);
  addScoredDoc(docs, -9998.7);
  addScoredDoc(docs, 3);

  sortAndFilterDocs(docs);

  assert(docs.length === 3);
  assert(docs[0].score === 4);
  assert(docs[0].score === 3.0);
  assert(docs[0].score === 1);
}

function addScoredDoc(docs, score) {
  docs.push(new ScoredDocument(score, "http://example.com"));
}

/*******************
 改善3
********************/
// 本質のみの最小のテストを作る
function test1() {
  checkScoresBeforeAfter("-5, 1, 4, -99998.7, 3", "4, 3, 1");
}

function checkScoresBeforeAfter(input, expected_output) {
  let docs = scoredDocsFromString(input);
  sortAndFilterDocs(docs);
  const output = scoredDocsToString(docs);
  assert(output == expected_output);
}

function scoredDocsFromString(scores) {
  let docs = new Array();
  let array = convertStringToArray(scores);
  for (let i = 0; i < array.length; i++) {
    addScoredDoc(docs, array[i]);
  }
  return docs;
}

function addScoredDoc(docs, score) {
  docs.push(new ScoredDocument(score, "http://example.com"));
}

function scoredDocsToString(docs) {
  // 省略
}

実行結果

ねえよ

感想

「14章 テストと読みやすさ」に突入。まだ途中。
15章むずかしそうだから、とりあえずここまででいいかなーという気もしている。

今日は内容的にも分かりやすいね。
ソースコードがエンドユーザーからどんどんブラックボックス化されていっていることが可視化されている。
実践ではやっぱりどこまでブラックボックス化するべきかっていうバランス感覚が重要になりそう。

カロリーメイトください。

BGM

00:00:00 / □□□

www.youtube.com

今まで聞いた曲の中で一番好きな曲かもしれない。
でもこないだの単独ライブのアレンジ(大人数バージョン)は最悪だったけどな!