2011-03-07 22 views
3

e.g有没有办法缩短包含一堆布尔比较的条件?

if("viewCategoryTree".equals(actionDetail) 
       || "fromCut".equals(actionDetail) 
       || "fromPaste".equals(actionDetail) 
       || ("viewVendorCategory".equals(actionDetail))&&"viewCategoryTree".equals(vendorCategoryListForm.getActionOrigin()) 
       || ("viewVendorCategory".equals(actionDetail))&&"fromEdit".equals(vendorCategoryListForm.getActionOrigin()) 
       || "deleteSelectedItem".equals(actionDetail) 
       || ("viewVendorCategory".equals(actionDetail))&&"fromLink".equals(vendorCategoryListForm.getActionOrigin())){ 
//do smth 
} 

我已经试过这样的事情

if(check("deleteSelectedItem,viewCategoryTree,fromCut,fromPaste,{viewVendorCategory&&viewVendorCategory},{viewVendorCategory&&fromEdit},{viewVendorCategory&&fromLink}",actionDetail,actionOrigin)){ 
//do smth 
} 

public boolean check(String str, String ad, String ao){ 

    String oneCmp = ""; 
    String[] result = str.split(","); 
    ArrayList adList = new ArrayList(); 
    ArrayList aoList = new ArrayList(); 
    for (int i=0; i<result.length; i++){ 
     oneCmp = result[i]; 
     Matcher m = Pattern.compile("\\{([^}]*)\\}").matcher(oneCmp); 
     if(m.matches()){ 
      m.find(); 
      String agrp = m.group(); 
      String[] groupresult = agrp.split("[\\W&&[^!]]+"); 
      Boolean a = false; 
      Boolean b = false; 
      if(groupresult[0].startsWith("!")){ 
       a = !groupresult[0].substring(1).equals(ad); 
      } else a = groupresult[0].equals(ad); 
      if(groupresult[1].startsWith("!")){ 
       b = !groupresult[1].substring(1).equals(ao); 
      }else b = groupresult[1].equals(ao); 

      if(agrp.indexOf("&&")!=-1){ 
       if(!(a && b))return false; 
      } 
      else if(agrp.indexOf("||")!=-1){ 
       if(!(a || b))return false; 
      } 
     } else { 
      if(oneCmp.indexOf("^")==-1){ 
       checklist(oneCmp,ad); 
         if(!checklist(oneCmp,ad))return false; 
      }else{ 
      if(!checklist(oneCmp,ao))return false; 
      } 
     } 
    } 

    return false; 
} 

public boolean checklist(String str, String key){ 

    if(str.startsWith("!")){ 
     if(str.substring(1).equals(key))return false; 
     }else { if (!str.substring(1).equals(key)) return false; 
     } 
    } 

    return false; 
} 

有没有更好的方式来做到这一点?谢谢。

+0

你可以使用某种“包含”或“in”功能吗? – Crisfole 2011-03-07 04:27:09

+0

你应该问这http://codereview.stackexchange.com – 2011-03-07 04:50:32

+0

@Oscar他* *可以*问这个问题,但它仍然是在这里的话题。 – Will 2011-03-07 11:41:52

回答

2

移动检查,这需要actionDetail作为参数的方法:

// Assumes vendorCategoryListForm is a member variable. 
boolean check(String actionDetail) { 
    return ("viewCategoryTree".equals(actionDetail) 
      || "fromCut".equals(actionDetail) 
      || "fromPaste".equals(actionDetail) 
      || (("viewVendorCategory".equals(actionDetail)) 
       &&"viewCategoryTree".equals(vendorCategoryListForm.getActionOrigin())) 
      || (("viewVendorCategory".equals(actionDetail)) 
       &&"fromEdit".equals(vendorCategoryListForm.getActionOrigin())) 
      || "deleteSelectedItem".equals(actionDetail) 
      || (("viewVendorCategory".equals(actionDetail)) 
       &&"fromLink".equals(vendorCategoryListForm.getActionOrigin()))) 
} 

if (check(actionDetail)) { 
    // do this 
} 
2

如何创造你需要测试反对什么的数组。 然后一些像这样的代码:

有关阵列的好处是,它很容易扩展:您可以轻松地添加/静态和动态删除元素吧。

0

老实说,这段代码不可读。我最好建议把这个条件支票封装到if (control.IsApplicable) { // do smth }这种类型的某个属性中。

无论要么你被一个或两个参数参数。 但我想更好的解决方案是有一个匹配数组,可以测试,如果匹配,然后返回true。

0

我不认为你要改善这种不添加一堆复杂的,无论是在你用它来表达条件和评估它们的“发动机”的实施符号的条款。

的符号问题是:虽然你可能最终表达更少的字符的条件,别人读你的代码必须弄清楚什么是时髦的字符串文字的真正含义。

此外,任何聪明的你可能会对性能产生影响。例如,您的尝试每次调用check多次编译和应用正则表达式。

棒与你有什么是我的忠告。

1

也请看看这个帖子

Language Agnostic贷Galwegian

Flattening Arrow Code寻求帮助。

1. Replace conditions with guard clauses. 
    2. Decompose conditional blocks into seperate functions. 
    3. Convert negative checks into positive checks. 
0
if(isValidActionDetail(actionDetail) 
      || (isValidActionDetail(actionDetail) 
      && ("viewCategoryTree".equals(vendorCategoryListForm.getActionOrigin()) 
       || "fromEdit".equals(vendorCategoryListForm.getActionOrigin()) 
       || "fromLink".equals(vendorCategoryListForm.getActionOrigin())))){ 

//do smth 
    } 
} 

public static boolean isValidActionDetail (String actionDetail) { 
    return "viewCategoryTree".equals(actionDetail) || "fromCut".equals(actionDetail) 
      || "fromPaste".equals(actionDetail) || "deleteSelectedItem".equals(actionDetail) 
      || "viewVendorCategory".equals(actionDetail); 
} 

您可以用上述方法分解,作为第一步重构你的逻辑。