什么是在检查条件,摆脱code嵌套IFS的最佳方式? [英] What's the best way to get rid of nested ifs in code while checking for conditions?

查看:105
本文介绍了什么是在检查条件,摆脱code嵌套IFS的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发在Java应用程序黑莓和我有一个选项类,其中存储所有用户设置。问题是我需要检查一些条件,以便知道如何作出反应。正如我不断加入更多的功能,更多的GUI选项显示给用户,更多的设置存储在选项类和更多的条件需要进行检查。

I'm developing a BlackBerry app in Java and I have an Options class where all user settings are stored. The problem is I need to check some conditions in order to know how to react. As I keep adding more features, more GUI options are shown to the user, more settings are stored in the Options class and more conditions need to be checked for.

看看下面code,例如:

Take the following code for example:

private void doCallMonitoring(int callId){
    /*This is the part that I want to avoid. Having
      multiple nested ifs. Here's just two conditions
      but as I add more, it will get unmantainable
      very quickly.*/
    if(Options.isActive().booleanValue()){
        callTime = new Timer();
        TimerTask callTimeTask = new TimerTask(){
            public void run(){
                callTimeSeconds++;
	    if((callTimeSeconds == Options.getSoftLimit().intValue()) && (Phone.getActiveCall().getStatus() == PhoneCall.STATUS_CONNECTED)){
                    injectDTMFTone(Phone.getActiveCall());
	    }else if((callTimeSeconds >= Options.getHardLimit().intValue()) && (Phone.getActiveCall().getStatus() == PhoneCall.STATUS_CONNECTED)){
                    injectEndCall();
                }
             }
        };     
        callTime.schedule(callTimeTask, 0,1000);
    }else{
    System.out.println("Service not active");
    }
}

我怎么想它的工作是验证所有选项与单个呼叫,并从那里决定行动的诅咒。我怎样才能实现这样的设计呢?

How I would want it to work is to verify all options with a single call and from there determine the curse of action. How can I achieve such a design?

推荐答案

另一个选择是使的方法,如 injectDMTFTone()检查,如果他们想搞定条件,并返回取决于它是否被处理或不真或假。

Another option is to make methods such as injectDMTFTone() check to see if they want to handle that condition, and return true or false depending on if it was handled or not.

例如:

public void run() {
    callTimeSeconds++;
    do {
        if (handleInjectDMTFTone())
            break;
        if (handleInjectEndCall())
            break;
    } while(false);

    callTime.schedule(callTimeTask, 0,1000);
}

boolean handleInjectDMTFTone() {
    if ((callTimeSeconds != Options.getSoftLimit().intValue()) ||
        (Phone.getActiveCall().getStatus() != PhoneCall.STATUS_CONNECTED))
        return false;

    injectDTMFTone(Phone.getActiveCall());
    return true;
}

boolean handleInjectEndCall() {

    if ((callTimeSeconds < Options.getHardLimit().intValue()) ||
        (Phone.getActiveCall().getStatus() != PhoneCall.STATUS_CONNECTED))
        return false;

    injectEndCall();
    return true;
}

当然,而不是调用另一个 injectDMTFTone()方法或 injectEndCall()方法,你只需在线在这些方法中的逻辑正确的。这样一来你的分组何时以及如何处理在同一个地方这些条件全部逻辑。

Of course, instead of calling another injectDMTFTone() method or injectEndCall() method, you would just inline that logic right in those methods. In that way you've grouped all the logic of how and when to deal with those conditions in the same place.

这是我最喜欢的模式之一;使用如果语句尽量靠近方法的顶部,是有道理的消除条件和返回。该方法的其余部分没有缩进层次多,简单易懂易读。

This is one of my favorite patterns; use if statements as close to the top of methods as makes sense to eliminate conditions and return. The rest of the method is not indented many levels, and is easy and straightforward to read.

您还可以通过创建所有实现相同的接口,并在处理程序的存储库,你的运行对象扩展该方法可以遍历,看看哪个会处理它。这可能是也可能不是矫枉过正,以你的情况。

You can further extend this by creating objects that all implement the same interface and are in a repository of handlers that your run method can iterate over to see which will handle it. That may or may not be overkill to your case.

这篇关于什么是在检查条件,摆脱code嵌套IFS的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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