多くの異なる .cpp ファイルで次のようなことを行う C++ プログラムがあります。
if (!thing1.empty() && !thing2.empty())
{
if (thing1.property < thing2.property)
return func1();
else if (thing2.property < thing1.property)
return func2();
else
return func3();
}
else if (!thing1.empty())
{
return func1();
}
else if (!thing2.empty())
{
return func2();
}
else
{
return func4();
}
私は、thing1 が thing2 よりも大きい場合は一方向に func を実行しようとしていますが、逆の場合は逆方向に実行しようとしていますが、存在しない場合はその半分に対してのみ func を実行します。どちらも存在しない場合は、まったく別のことを行います。プロパティ、関数、および戻り値の型は、このパターンを使用するたびに異なります。このネストされた if ステートメントの醜い混乱よりも、私がやりたいことのためのより良い設計はありますか?
編集:私のコード例は単純化しすぎていることに気付きました。これは、うまくいけば問題をよりよく説明する私の実際のコードの一部です(ただし、はるかに面倒です):
if (!diamondsOnly.empty() && !clubsOnly.empty())
{
if (diamondsOnly.size() < clubsOnly.size())
{
if (passHighCards(player.hand, getHighCards(Card::DIAMONDS), result))
return result;
if (passHighCards(player.hand, getHighCards(Card::CLUBS), result))
return result;
}
else if (clubsOnly.size() < diamondsOnly.size())
{
if (passHighCards(player.hand, getHighCards(Card::CLUBS), result))
return result;
if (passHighCards(player.hand, getHighCards(Card::DIAMONDS), result))
return result;
}
else
{
if (diamondsOnly.back().value > clubsOnly.back().value)
{
if (passHighCards(player.hand, getHighCards(Card::DIAMONDS), result))
return result;
if (passHighCards(player.hand, getHighCards(Card::CLUBS), result))
return result;
}
else
{
if (passHighCards(player.hand, getHighCards(Card::CLUBS), result))
return result;
if (passHighCards(player.hand, getHighCards(Card::DIAMONDS), result))
return result;
}
}
}
else if (!diamondsOnly.empty())
{
if (passHighCards(player.hand, getHighCards(Card::DIAMONDS), result))
return result;
}
else if (!clubsOnly.empty())
{
if (passHighCards(player.hand, getHighCards(Card::CLUBS), result))
return result;
}