Suppose I have a long, complex list of conditions, which must be true in order for an if statement to run.
if(this == that && foo != bar && foo != that && pins != needles && apples != oranges)
{
DoSomethingInteresting();
}
Typically, if I'm forced into doing something like this, I'll just put each statement on its own line, like this:
if
(
this == that
&& foo != bar
&& foo != that
&& pins != needles
&& apples != oranges
)
{
DoSomethingInteresting();
}
But I still feel this is a bit of a mess. I'm tempted to refactor the contents of the if statement into its own property like this
if(canDoSomethingInteresting)
{
DoSomethingInteresting();
}
But then that just moves all the mess into canDoSomethingInteresting()
and doesn't really fix the problem.
As I said, my goto solution is the middle one, because it doesn't obfuscate the logic like the last one and is more readable than the first. But there must be a better way!
Example in response to Sylon's comment
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();
}