大量if / else语句的最佳实践 [英] Best practice for large amounts of if/else statements

查看:138
本文介绍了大量if / else语句的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含大量if / else语句的应用。 if / else语句也包含randomnumber,例如if语句可以包含多个选项。我可以为这种类型的块使用NSDictionary,还是至少有一些方法将它放在一个单独的文件中以清理mainViewController? if / else语句的代码块返回到mainViewController的两个部分。无论如何,这只是代码的一小部分:

Im creating an app with a large amount of if/else statements. Also the if/else statements contains a randomnumber where for example an if statement can contain several options. Can I use a NSDictionary for this type of blocks or is there at least some way to put it in a seperate file to "clean" up the mainViewController? The if/else statement chunk of code comes back in two parts of my mainViewController. Anyway, this is a small part of the code:

if ([newCondition.number floatValue] >= 15.0f) {
         if (randomNumber == 1){
         [mainLabel setText:@"THIS IS"];
         [label1 setText:@"ONE"];
         [label2 setText:@"WAY TO GO"];
         } else {
         [mainLabel setText:@"THIS IS"];
         [label1 setText:@"THE RIGHT"];
         [label2 setText:@"WAY TO DO IT"]; }

     } else if ([newCondition.number floatValue] <=1.0f && [newCondition.anothernumber  
floatValue] >= 90.0f) {
         if (randomNumber == 0) {
         [mainLabel setText:@"THIS ISN´T"];
         [label1 setText:@"GOOD"];
         [label2 setText:@""];
         } else {
         [mainLabel setText:@"ARE"];
         [label1 setText:@"YOU"];
         [label2 setText:@"DONE?"]; }

     } else if ([newCondition.number floatValue] >=30.0f && [newsCondition.anothernumber  
floatValue] >= 80.0f) {
         [mainLabel setText:@"CAN"];
         [label1 setText:@"YOU"];
         [label2 setText:@"STOP?"];


推荐答案

首先,好的注意到大的if / else陈述不是积极的事情。有很多方法可以让它们减少痛苦,更容易阅读,其中一些你已经触及过。例如,您可以将其移动到另一个类(如您所说),或者至少将其移动到其他代码中的另一个方法。

First off, good job noticing that large if/else statements are not a positive thing. There are a lot of ways to make them less painful and easier to read, some of which you've touched on. For example, you could move this to another class (as you said), or at the very least another method to isolate them from the rest of your code.

一个设计我发现在这种情况下有用的模式是责任链模式。在CoR模式中,您有许多命令对象,负责了解他们是否可以处理一个特定的价值。因此,在您的情况下,您的逻辑中的每个if / else都有一个命令对象,并且每个命令对象都知道它们是否可以处理newCondition.number。如果命令对象可以处理该值,它将执行其逻辑(在您的情况下,它将执行if / else中的内容);如果它不能,它会将消息传递给链中的下一个对象。

One design pattern that I've found helpful in this situation is the chain-of-responsibility pattern. In the CoR pattern, you have a number of command objects that are responsible for knowing if they can handle a particular value. So in your case, you'd have a command object for each if/else in your logic, and each command object would know if they can handle newCondition.number. If the command object can handle the value, it performs its' logic (in your case, it'd perform the stuff inside the if/else); if it cannot, it passes the message along to the next object in the chain.

这样做的好处是可以隔离逻辑并使其更容易添加功能而且影响最小。您还可以将命令子类命名为有趣且有用的信息,以消除代码的一些神秘感。

This has the benefit of isolating the logic and making it easier to add functionality with minimal impact. You can also name the command subclasses something interesting and informative to remove some of the mystique of the code.

至少,我将if语句重构为其自己的方法。如果你有很多if / else语句,我会考虑责任链模式。

At the very least, I'd refactor the if statement into its own method. And if you have lots of if/else statements, I'd consider the chain-of-responsibility pattern.

有一本名为重构模式我强烈推荐。

There is a great book called Refactoring to Patterns that I highly recommend.

这篇关于大量if / else语句的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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