### 你如何处理巨大的条件？

``````if (var1 = true && var2 = true && var2 = true && var3 = true && var4 = true && var5 = true && var6 = true)
{
``````

``````if (var1 = true && var2 = true && var2 = true
&& var3 = true && var4 = true && var5 = true
&& var6 = true)
{
``````

``````if (var1 = true && var2 = true && var2 = true && var3 = true)
{
if (var4 = true && var5 = true && var6 = true)
{
``````

#### 21 个答案:

``````bool isOpaque = object.Alpha == 1.0f;
bool isDrawable = object.CanDraw && object.Layer == currentLayer;
bool isHidden = hideList.Find(object);

bool isVisible = isOpaque && isDrawable && ! isHidden;

if(isVisible)
{
// ...
}
``````

``````public bool IsVisible {
get
{
bool isOpaque = object.Alpha == 1.0f;
bool isDrawable = object.CanDraw && object.Layer == currentLayer;
bool isHidden = hideList.Find(object);

return isOpaque && isDrawable && ! isHidden;
}
}

void Draw()
{
if(IsVisible)
{
// ...
}
}
``````

“可读性”解决方案是一种风格问题，因此可以解释。我的偏好是：

``````if (var1 == true && // Explanation of the check
var2 == true && // Explanation of the check
var3 == true && // Explanation of the check
var4 == true && // Explanation of the check
var5 == true && // Explanation of the check
var6 == true)   // Explanation of the check
{ }
``````

``````if (var1 && // Explanation of the check
var2 && // Explanation of the check
var3 && // Explanation of the check
var4 && // Explanation of the check
var5 && // Explanation of the check
var6)   // Explanation of the check
{ }
``````

``````/// <Summary>
/// Tests whether all the conditions are appropriately met
/// </Summary>
private bool AreAllConditionsMet (
bool var1,
bool var2,
bool var3,
bool var4,
bool var5,
bool var6)
{
return (
var1 && // Explanation of the check
var2 && // Explanation of the check
var3 && // Explanation of the check
var4 && // Explanation of the check
var5 && // Explanation of the check
var6);  // Explanation of the check
}

private void SomeMethod()
{
// Do some stuff (including declare the required variables)
if (AreAllConditionsMet (var1, var2, var3, var4, var5, var6))
{
// Do something
}
}
``````

``````bool orderValid = orderDate < DateTime.Now && orderStatus != Status.Canceled;
bool custValid = customerBalance == 0 && customerName != "Mike";
if (orderValid && custValid)
{
...
``````

``````public void doSomething() {
if (condition1 && condition2 && condition3 && condition4) {
// do something
}
}
``````

``````public void doSomething() {
if (!condition1) {
return;
}

if (!condition2) {
return;
}

if (!condition3) {
return;
}

if (!condition4) {
return;
}

// do something
}
``````

``````if (var1 == true
&& var2 == true
&& var3 == true
) {
/* do something.. */
}
``````

``````if (var1 == true
&& var2 == true
&& var3 == true) {
/* do something.. */
}
``````

``````import org.apache.commons.collections.ClosureUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.functors.NOPClosure;

Map predicateMap = new HashMap();

predicateMap.put( isProblem, flagForAttention );
predicateMap.put( null, ClosureUtils.nopClosure() );

Closure processStudents =
ClosureUtils.switchClosure( predicateMap );

CollectionUtils.forAllDo( allStudents, processStudents );
``````

``````import org.apache.commons.collections.Closure;
import org.apache.commons.collections.Predicate;

// Anonymous Predicate that decides if a student
// has made the honor roll.
Predicate isHonorRoll = new Predicate() {
public boolean evaluate(Object object) {
Student s = (Student) object;

return( ( s.getGrade().equals( "A" ) ) ||
s.getAttendance() == PERFECT ) );
}
};

// Anonymous Predicate that decides if a student
// has a problem.
Predicate isProblem = new Predicate() {
public boolean evaluate(Object object) {
Student s = (Student) object;

return ( ( s.getGrade().equals( "D" ) ||
s.getStatus() == SUSPENDED );
}
};

// Anonymous Closure that adds a student to the
// honor roll
Closure addToHonorRoll = new Closure() {
public void execute(Object object) {
Student s = (Student) object;

// Add an award to student record
Database.saveStudent( s );
}
};

// Anonymous Closure flags a student for attention
Closure flagForAttention = new Closure() {
public void execute(Object object) {
Student s = (Student) object;

// Flag student for special attention
s.addNote( "talk to student", 2005 );
s.addNote( "meeting with parents", 2005 );
Database.saveStudent( s );
}
};
``````

if（var1＆amp;＆amp; var2＆amp;＆amp; var2＆amp;＆amp; var3＆amp;＆amp; var4＆amp;＆amp; var5＆amp;＆amp; var6）{
......

boolean ok = cond1;
ok＆amp; = cond2;
ok＆amp; = cond3;
ok＆amp; = cond4;
ok＆amp; = cond5;
ok＆amp; = cond6;

ok =（cond1＆amp;＆amp; cond2＆amp;＆amp; cond3＆amp;＆amp; cond4＆amp;＆amp; cond5＆amp;＆amp; cond6）;

``````Bool cond1 == (var1 && var2);
Bool cond2 == (var3 && var4);

if ( cond1 && cond2 ) {}
``````

``````\$vars = array('var1', 'var2', ... etc.);
foreach (\$vars as \$v)
if (\$\$v == true) {
// do something
break;
}
``````

``````if (var1 = true
&& var2 = true
&& var2 = true
&& var3 = true
&& var4 = true
&& var5 = true
&& var6 = true){
``````

``````if ((var1 = true && var2 = true)
&& ((var2 = true && var3 = true)
&& (var4 = true && var5 = true))
&& (var6 = true)){
``````

``````>>> L = [True, True, True, False, True]
>>> all(L) # True, only if all elements of L are True.
False
>>> any(L) # True, if any elements of L are True.
True
``````

@tweakt

boolean ok = cond1;      ok＆amp; = cond2;      ok＆amp; = cond3;      ok＆amp; = cond4;      ok＆amp; = cond5;      ok＆amp; = cond6;

ok =（cond1＆amp;＆amp; cond2＆amp;＆amp; cond3＆amp;＆amp; cond4＆amp;＆amp; cond5＆amp;＆amp; cond6）;

``````do {
if (!cond1)
break;
if (!cond2)
break;
if (!cond3)
break;
...
DoSomething();
} while (false);
``````

while（false）有点俗气。我希望语言有一个名为“一次”的范围操作符或者你可以轻易打破的东西。

``````bool isVar1Valid, isVar2Valid, isVar3Valid, isVar4Valid;
isVar1Valid = ( var1 == 1 )
isVar2Valid = ( var2.Count >= 2 )
isVar3Valid = ( var3 != null )
isVar4Valid = ( var4 != null && var4.IsEmpty() == false )
if ( isVar1Valid && isVar2Valid && isVar3Valid && isVar4Valid ) {
//do code
}
``````

``````{
last unless \$var1;
last unless \$var2;
last unless \$var3;
last unless \$var4;
last unless \$var5;
last unless \$var6;

... # Place Code Here
}
``````

``````    if (   (condition_A)
&& (condition_B)
&& (condition_C)
&& (condition_D)
&& (condition_E)
&& (condition_F)
)
{
...
}
``````

``````    if (condition_A) {
if (condition_B) {
if (condition_C) {
if (condition_D) {
if (condition_E) {
if (condition_F) {
...
}
}
}
}
}
}
``````

``````    if (   (   (condition_A)
&& (condition_B)
)
|| (   (condition_C)
&& (condition_D)
)
|| (   (condition_E)
&& (condition_F)
)
)
{
do_this_same_thing();
}
``````

``````    if (condition_A && condition_B) {
do_this_same_thing();
}
if (condition_C && (condition_D) {
do_this_same_thing();
}
if (condition_E && condition_F) {
do_this_same_thing();
}
``````

``````    if (expr_A & expr_B || expr_C | expr_D & expr_E || expr_E && expr_F & expr_G || expr_H {
}
``````

``````    if (   (  (expr_A)
& (expr_B)
)
|| (  (expr_C)
| (  (expr_D)
& (expr_E)
)
)
|| (   (expr_E)
&& (  (expr_F)
& (expr_G)
)
)
|| (expr_H)
)
{
}
``````

``````if (var1 == true) {
if (var2 == true) {
if (var3 == true) {
...
}
}
}
``````