提高对条件语句的可读性 [英] Improving legibility on conditional statement

查看:171
本文介绍了提高对条件语句的可读性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个HTTP服务器,我的Andr​​oid设备。

I am building a HTTP server for my android device.

我使用了很多if-else语句来处理请求不同的充

I am using a lot of IF-ELSE statements to handle differnt requests.

我将与其他人供以后使用可以分享我的code,我将不得不作出它作为清晰越好。现在,我甚至不能看我的code自如。

As I will be sharing my code with other people for later use, I will have to make it as legible as possible. Right now, I can't even read my code with ease.

我认为问题来自于一个类使用了大量的if-else语句。
例如。

I think the problem comes from using a lot of IF-ELSE statements in one class. For example.

if(purpose.equals("readProfile"){
     .....
}
else if(purpose.equals("writeProfile"){
     .....
}
    ....

我想他们在类别进行分类,并根据其类别排序的条件。但不是很多legebility的得到改善。
然后我试着写短的注释盈的每个条件。但是,这变得更加混乱。

I tried classifying them in category and ordered the conditions according to their category. But not a lot of legebility was improved. Then I tried writing short comments infront of each conditions. But this made even more mess.

什么可以做,以增加可读性的条件语句?

What can be done to increase legibility for conditional statements?

推荐答案

Luiggi门多萨说,这是跟进一个previous问题 ...

As Luiggi Mendoza stated, this is a follow up of a previous question...

如果您正在使用的 Java 7中,您可以使用的字符串的switch-case语句

If you are using Java 7, you can use a switch-case statement for strings

    //month is a String
    switch (month.toLowerCase()) {
        case "january":
            monthNumber = 1;
            break;
          //partsleft out for sake of brevity ..
        default: 
            monthNumber = 0;
            break;
    }

(节选从Oracle Java教程,上面提到的。)

(excerpt from the Oracle Java Tutorials, referenced above.)

然而,这个巨大的if-else只是问题的一部分。由于这似乎是随着时间而长势的结构,我建议你彻底的重构,用什么对我来说是一个的策略模式的。你应该:

However, this huge if-else is just part of the problem. As this seems to be a structure growing over time, I'd recommend a thorough refactoring, and using what seems to me is a Strategy pattern. You should:

制定覆盖边界为所有用例的接口:

Formulate an interface which covers the boundaries for all the use cases:

interface MyStrategy {
  void execute(MyInputContext input, MyOutputContext output);
}

(使用MyInputContext和MyOutputContext只是一种方法的空隙的方法,这仅仅是一个例子,但是,以处理具有响应请求,这是有意义的,就像Servlet的工作方式)

(using a void method with MyInputContext and MyOutputContext are just one approach, this is just an example, but to handle requests that have responses, this makes sense, just like how Servlets work)

重构大的if-else语句的内容到这个接口的实例(这将是策略):

Refactor the content of the big IF-ELSE statement into instances of this interface (these will be the strategies):

//VERY simplified...
class ReadProfileStrategy implements MyStrategy {
  void execute(MyInputContext input, MyOutputContext output) {
    //do the stuff that was in the if-else block in the "readProfile" part
  }
}

//... at the branching part:
MyInputContext input; //build this here
MyOutputContext output; //build this here

switch (purpose) {
    case "readProfile":
         // no need to always instantiate this, it should be stateless...
         new ReadProfileStrategy().execute();
         break;
    //... left out for sake of brevity
}

重构第2步

如果做到这一点,您可以将字符串ID添加到界面,与实例本身,并得到完全摆脱的if-else或switch语句中,你可以创造出通过IOC容器填充一个Map(如) ,是最新的,并完全灵活

Refactoring step 2

If this is done, you can add the string IDs to the interface, and the instances themselves, and get rid of the if-else or switch statement altogether, you could create a Map populated even through an IOC container (like), to be up to date, and completely flexible.

class ReadProfileStrategy implements MyStrategy {
  String getID() {
      return "readProfile";
  }

  void execute(MyInputContext input, MyOutputContext output) {
    //do the stuff that was in the if-else block in the "readProfile" part
  }
}

在请求进行处理时的类

private final Map<String, MyStrategy> strategyMap; //fill the map using your favorite approach, like using Spring application context, using the getCode() to provide the key of the map

在处理逻辑:

MyStrategy strategy = strategyMap.get(purpose);
if(strategy!=null) {
    strategy.execute();
}
else {
    //handle error here
}

这篇关于提高对条件语句的可读性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆