反如果运动 [英] anti-if campaign

查看:17
本文介绍了反如果运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个非常有趣的网站,该网站表达了一个非常有趣的想法 - anti-if 活动.您可以在 www.antiifcampaign.com 上看到这一点.我不得不同意复杂的嵌套 IF 语句在后面是绝对的痛苦.我目前正在做的一个项目直到最近有一些疯狂的嵌套 IF,它们向右滚动了很多方式.我们通过两种方式解决了我们的问题 - 我们使用 Windows Workflow Foundation 来解决路由(或工作流)问题.我们正在利用适用于 .NET 的 ILOG 规则(最近由 IBM 购买!!)实施我们的所有业务规则.这在很大程度上治愈了我们嵌套的 IF 痛苦......但我发现自己想知道有多少人以 AntiIfCampaign 的好人建议的方式治愈他们的痛苦(

I recently ran against a very interesting site that expresses a very interesting idea — the anti-if campaign. You can see this here at www.antiifcampaign.com. I have to agree that complex nested IF statements are an absolute pain in the rear. I am currently on a project that up until very recently had some crazy nested IFs that scrolled to the right for quite a ways. We cured our issues in two ways - we used Windows Workflow Foundation to address routing (or workflow) concerns. And we are in the process of implementing all of our business rules utilizing ILOG Rules for .NET (recently purchased by IBM!!). This for the most part has cured our nested IF pains...but I find myself wondering how many people cure their pains in the manner that the good folks at the AntiIfCampaign suggest (see an example here) by creating numerous amounts of abstract classes to represent a given scenario that was originally covered by the nested IF. I wonder if another way to address the removal of this complexity might also be in using an IoC container such as StructureMap to move in and out of different bits of functionality. Either way...

问题: 假设我有一个嵌套的复杂 IF 或 SWITCH 语句,用于评估给定类型的事物(例如评估 Enum)以确定我想要如何处理处理枚举类型的那个东西 - 有哪些方法可以在不使用 IF 或 SWITCH 层次结构的情况下进行相同形式的处理?

Question: Given a scenario where I have a nested complex IF or SWITCH statement that is used to evaluate a given type of thing (say evaluating an Enum) to determine how I want to handle the processing of that thing by enum type - what are some ways to do the same form of processing without using the IF or SWITCH hierarchical structure?

public enum WidgetTypes
{
    Type1,
    Type2,
    Type3,
    Type4
}

...

WidgetTypes _myType = WidgetTypes.Type1;

...

switch(_myType)
{
    case WidgetTypes.Type1:
        //do something
        break;

    case WidgetTypes.Type2:
        //do something
        break;

    //etc...
}

推荐答案

问题不在于'if'语句,而在于写出糟糕代码的程序员.

The problem is not the 'if' statement, it is the programmers who write bad code.

另外,正如其他人所指出的,当您使用 if 语句检查对象的 type 时,您应该使用多态性(如果可用),但 if 语句本身是非常有用的基本结构.

Also, as others have pointed out, you should be using polymorphism (if available) when you are using if statements to check the type of an object, but if statements in and of themselves are very useful and fundamental constructs.

这篇关于反如果运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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