减少 If 语句中的代码重复

发布于 2024-12-17 00:00:43 字数 862 浏览 2 评论 0原文

我有相当大的代码块,用于生成/返回由多个数据源构造的搜索结果的数组列表。我被要求包含一个日期限制方法,用户可以根据 2 个日期字段构建数组。如果填充了这些日期字段之一,假设其名为 dateFrom,则数组将返回指定日期之后的数据。类似地,如果填充了 dateTo,那么它将用前面的数据填充数组,如果两者都填充,那么 if 显然会根据该范围进行填充。

这一切都很好&使用几个简单的嵌套 if 语句或 switch case 很容易做到,但我的问题是,这也意味着我将不得不重复大量代码。有没有办法可以防止相同的基本数组列表构造代码重复 4 次?我知道这是可怕的伪代码,但它明白了我的观点:

if(condition1){
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
} else if (condition2) {
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
} else if (condition3) {
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
} else {
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
}

return ArrayList();

值得注意的是,虽然这更像是一个广义的 Java 问题,但我正在使用 JSF2 框架,并且此方法发生在 ViewScoped bean 和 ViewScoped bean 中。使用@PostContruct标签实现。

干杯

I have a fairly large chunk of code that produces/returns an arraylist of search results that is constructed from a number of datasources. I have been asked to include a date restriction method whereby users can build the array based off 2 date fields. If one of these date fields is populated, lets say its called dateFrom then the array will return data from after that date specified. Similarly if the dateTo is populated then it will populate the array with preceeding data and if both then if will obviously populate based off that range.

This is all nice & easy to do with a couple of simple nested if statements or a switch case, but my problem is that this will also mean that I will have to repeat hefty chunk of code. Is there a way I can prevent the same basic arraylist construction code being repeated 4 times? I know this is horrible psuedo code but it gets my point accross:

if(condition1){
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
} else if (condition2) {
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
} else if (condition3) {
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
} else {
  setItem1(getMethod());
  setItem2(getMethod());
  setItem3(getMethod());
}

return ArrayList();

Its worth noting that although this is more a generalised Java question I am using JSF2 framework and this method occurs in a ViewScoped bean & is implemented using the @PostContruct tag.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2024-12-24 00:00:43

我建议将 if 语句移至另一个方法以减少耦合,然后将 setItems 移至另一个方法并将 getmethods 的结果作为参数发送。 (如果修改对象的字段会更好)
只是伪代码:

 return createArray();

 private List createArray()
 {
     if(condition1){
        setItems(getMethod(),getMethod(),getMethod());
        return ArrayList();
     }
     if (condition2) {
        setItems(getMethod(),getMethod(),getMethod());
        return ArrayList();
     } 
     ...
 }

 private void setItems(Object var1, Object var2, Object var3)
 {
     ...
 }

如果你想完全摆脱 if ,你可以使用多态性和依赖注入,但在这种情况下它可能有点矫枉过正......

I suggest moving the if statement to another method to decrease coupling and then moving the setItems to another method and sending the results of getmethods as parameters. (better if you modify the fields of an object)
just pseudocode:

 return createArray();

 private List createArray()
 {
     if(condition1){
        setItems(getMethod(),getMethod(),getMethod());
        return ArrayList();
     }
     if (condition2) {
        setItems(getMethod(),getMethod(),getMethod());
        return ArrayList();
     } 
     ...
 }

 private void setItems(Object var1, Object var2, Object var3)
 {
     ...
 }

If you want to get rid of the if completely, you can use polymorphism and dependency injection, but it can be overkill in this case...

隔纱相望 2024-12-24 00:00:43

我是不是

if(condition1 || condition2 || condition3 ||condition4){
    stuff
}

错过了什么?

How about

if(condition1 || condition2 || condition3 ||condition4){
    stuff
}

Am I missing something?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文