長くて複雑な条件のリストがあるとします。これは、ifステートメントを実行するためにtrueである必要があります。
if(this == that && foo != bar && foo != that && pins != needles && apples != oranges)
{
DoSomethingInteresting();
}
通常、このようなことを強いられた場合は、次のように各ステートメントを独自の行に配置します。
if
(
this == that
&& foo != bar
&& foo != that
&& pins != needles
&& apples != oranges
)
{
DoSomethingInteresting();
}
しかし、私はまだこれが少し混乱していると感じています。ifステートメントの内容を次のような独自のプロパティにリファクタリングしたいと思います
if(canDoSomethingInteresting)
{
DoSomethingInteresting();
}
しかし、それはすべての混乱を引き起こしcanDoSomethingInteresting()
、実際には問題を解決しません。
私が言ったように、私のgotoソリューションは真ん中のソリューションです。これは、最後のソリューションのようにロジックを難読化せず、最初のソリューションよりも読みやすいためです。しかし、もっと良い方法があるはずです!
Sylonのコメントに対する回答の例
bool canDoSomethingInteresting
{
get{
//If these were real values, we could be more descriptive ;)
bool thisIsThat = this == that;
bool fooIsntBar = foo != bar;
bool fooIsntThat = foo != that;
return
(
thisIsThat
&& fooIsntBar
&& fooIsntThat
);
}
}
if(canDoSomethingInteresting)
{
DoSomethingInteresting();
}