さて、この非常に単純なボタンクリック方法から始めましょう
private void button1_Click(object sender, EventArgs e)
{
int counter = 1;
List<int> items = new int[] { 1, 2, 3 }.ToList();
List<int>.Enumerator enm = items.GetEnumerator();
// 1
if (!enm.MoveNext())
throw new Exception("Unexpected end of list");
if (enm.Current != counter)
throw new Exception(String.Format("Expect {0} but actual {1}", counter, enm.Current));
counter++;
// 2
if (!enm.MoveNext())
throw new Exception("Unexpected end of list");
if (enm.Current != counter)
throw new Exception(String.Format("Expect {0} but actual {1}", counter, enm.Current));
counter++;
//3
if (!enm.MoveNext())
throw new Exception("Unexpected end of list");
if (enm.Current != counter)
throw new Exception(String.Format("Expect {0} but actual {1}", counter, enm.Current));
counter++;
if (enm.MoveNext())
throw new Exception("Unexpected continuation of list");
}
すべてのアサーションが適切にパスするため、このメソッドは何もしません。冗長性を取り除く方法を導入する必要があると考えるまでは問題ありません
static void AssertNext(ref int counter, List<int>.Enumerator e)
{
if (!e.MoveNext())
throw new Exception("Unexpected end of list");
if (e.Current != counter)
throw new Exception(String.Format("Expect {0} but actual {1}", counter, e.Current));
counter++;
}
private void button2_Click(object sender, EventArgs e)
{
var counter = 1;
var items = new int[] { 1, 2, 3 }.ToList();
var enm = items.GetEnumerator();
AssertNext(ref counter, enm);
AssertNext(ref counter, enm);
AssertNext(ref counter, enm);
if (enm.MoveNext()) throw new Exception("Unexpected continuation of list");
}
とはいえ、このリファクタリングは簡単です (少なくとも私にとっては)。それはプログラムを壊します!AssertNext への 2 回目の呼び出しでは、列挙子が既に開始点にリセットされ、アサーションが失敗したようです。
何が起こるか理解できません。私はこのパズルの初心者のように感じます。
ここで何が恋しいですか?