if else语句的代码清理

时间:2011-05-25 08:23:29

标签: java performance

这是代码的和平,我希望它能提高效率 我看到很多 if else语句

任何人都可以帮助我简化并减少代码行数

if(mmPageCounter.equalsIgnoreCase("trueNotFull")){
    if(whatPageNext.equalsIgnoreCase("first"))
        accountListSize = 5;
    else
        accountListSize = acctIdList.size();
}else if(mmPageCounter.equalsIgnoreCase("true")){
    if(whatPageNext.equalsIgnoreCase("first"))
        accountListSize = 5;
    else
        accountListSize = 10;
}else if(mmPageCounter.equalsIgnoreCase("false")){
    if(whatPageNext.equalsIgnoreCase("alwaysFirst"))    
        accountListSize = acctIdList.size();
}

在上面的代码基于mmPageCounter基于我设置的值 For循环的索引要么是满容量要么是实际值

满容量意味着,for循环将只迭代10个项目

ps:我的代码工作正常,但我只是在寻找微调

如果任何人有任何微调提示,请分享链接。

4 个答案:

答案 0 :(得分:1)

我会反过来写。 帐户大小应设为10? 帐户大小应设置为5?

if(shouldBeFive()){
  accountListSize = 5;
} else if (shouldBeTen()){
  accountListSize = 10;
} else{
  accountListSize = acctIdList.size()
}

你也可以在java7中切换大小写。

答案 1 :(得分:1)

这不会使它更快但减少代码,您可以替换:

if(whatPageNext.equalsIgnoreCase("first"))
    accountListSize = 5;
else
    accountListSize = 10;

由:

accountListSize = whatPageNext.equalsIgnoreCase("first") ? 5 : 10;

if(whatPageNext.equalsIgnoreCase("first"))
    accountListSize = 5;
else
    accountListSize = acctIdList.size();

由:

accountListSize = whatPageNext.equalsIgnoreCase("first") ? 5 : acctIdList.size();

更新:

如果mmPageCounter只能是“trueNotNull”,“true”或“false”,那么我认为这段代码是等效的:

int maxsize = mmPageCounter.equalsIgnoreCase("true") ? 10 : acctIdList.size();

if(mmPageCounter.equalsIgnoreCase("false")){
    if(whatPageNext.equalsIgnoreCase("alwaysFirst"))    
        accountListSize = accountListSize = acctIdList.size();
} else {
  accountListSize = whatPageNext.equalsIgnoreCase("first") ? 5 : maxsize
}

但你应该针对当前代码编写测试,并检查此代码在边缘情况下的行为方式是否完全相同。

答案 2 :(得分:0)

如果您事先知道mmPageCounterwhatPageNext的所有可能值,则可以使用HashMap

初始化期间:

accountListSizes = new HashMap<String, Integer>();
accountListSizes.put("trueNotFull|first", 5);
accountListSizes.put("true|first", 5);
...

计算期间:

String key = String.format("%s|%s", mmPageCounter, whatPageNext);
Integer value = accountListSizes.get(key);
if (value == null) {
    accountListSize = acctIdList.size();
} else {
    accountListSize = value;
}

此实现比您的实现慢一点,但它更容易阅读和扩展。您甚至可以在运行时更改映射。

答案 3 :(得分:0)

我的建议不是让你的代码更短,而是更易于扩展和更容易维护。但它会使客户端代码更短

我会选择一个enum但没有switch语句,因为我可以在这里找到一个策略,我更愿意将逻辑封装在客户端之外。另外,我提供了一个抽象(一个接口),允许我或其他人在将来使用替代实现,而不向enum添加新的常量。我将字符串和数字的字面值定义为常量(在抽象中,如果有意义的话)

这是“策略”抽象:

interface PageCounter {
    String FIRST = "first";
    String ALWAYS_FIRST = "alwaysFirst";

    int FIRST_SIZE = 5;
    int DEFAULT_SIZE = 10;

    int getAccountListSize(String whatPageNext, List<?> acctIdList);
}

这是实现enum接口并提供默认实现的PageCounter

enum DefaultPageCounter implements PageCounter {
    TRUENOTFULL {
        @Override
        public int getAccountListSize(String whatPageNext, List<?> acctIdList) {
            return FIRST.equalsIgnoreCase(whatPageNext) ? FIRST_SIZE : acctIdList.size();
        }
    },
    TRUE {
        @Override
        public int getAccountListSize(String whatPageNext, List<?> acctIdList) {
            return FIRST.equalsIgnoreCase(whatPageNext) ? FIRST_SIZE : DEFAULT_SIZE;
        }
    },
    FALSE {
        @Override
        public int getAccountListSize(String whatPageNext, List<?> acctIdList) {
            // some default (non)value is required here: -1 can be whatever.
            return ALWAYS_FIRST.equalsIgnoreCase(whatPageNext) ? acctIdList.size() : -1;
        }
    };
}

这是你的代码(在客户端中更短,并且不知道现有的可能性)

PageCounter counter = DefaultPageCounter.valueOf(mmPageCounter.toUpperCase());
int accountListSize = counter.getAccountListSize(whatPageNext, acctIdList);

可以通过某种工厂方法提供counter,以便您可以切换子类的PageCounter实现(这就是我选择使用接口的原因),但我不想使这比现​​在更复杂。