C#中简化丑陋嵌套if-else树的方法 [英] Methods of simplifying ugly nested if-else trees in C#

查看:362
本文介绍了C#中简化丑陋嵌套if-else树的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我在C#3.5中编写难看的if-else语句;我知道通过表驱动的开发,类层次结构,匿名方法以及其他一些简化方法。
的问题是,与传统的丑陋if-else语句相比,替代方法的传播范围仍然较小,因为没有约定。

Sometimes I'm writing ugly if-else statements in C# 3.5; I'm aware of some different approaches to simplifying that with table-driven development, class hierarchy, anonimous methods and some more. The problem is that alternatives are still less wide-spread than writing traditional ugly if-else statements because there is no convention for that.

嵌套的深度是多少if-else对于C#3.5是否正常?您希望看到什么方法而不是嵌套if-else?第二个?

What depth of nested if-else is normal for C# 3.5? What methods do you expect to see instead of nested if-else the first? the second?

如果我有10个输入参数,每个输入参数中有3个状态,我应该将函数映射到每个参数的每个状态的组合(确实要少一些,因为不是所有状态有效,但有时仍然很多)。我可以将这些状态表示为哈希表键和一个处理程序(lambda),如果键匹配,该处理程序将被调用。

if i have ten input parameters with 3 states in each, i should map functions to combination of each state of each parameter (really less, because not all the states are valid, but sometimes still a lot). I can express these states as a hashtable key and a handler (lambda) which will be called if key matches.

它仍然是表驱动和数据驱动的开发人员的混合体。想法和模式匹配。

It is still mix of table-driven, data-driven dev. ideas and pattern matching.

我正在寻找的是C#扩展的脚本编写方法(C#3.5类似于脚本编写)
http://blogs.msdn.com/ericlippert/archive/2004/02/24/79292。 aspx

what i'm looking for is extending for C# such approaches as this for scripting (C# 3.5 is rather like scripting) http://blogs.msdn.com/ericlippert/archive/2004/02/24/79292.aspx

推荐答案

有非常古老的形式主义,试图封装极其复杂的表达式,这些表达式评估许多可能独立的变量,例如决策表:

There are very old "formalisms" for trying to encapsulate extremely complex expressions that evaluate many possibly independent variables, for example, "decision tables" :

http://en.wikipedia.org/wiki/Decision_table

但是,我将在这里加入合唱团,以支持明智使用的观点如果可能的话,确定三元运算符,找出最不可能的条件,如果满足这些条件,则可以通过将其排除在外来终止评估的其余部分,然后加上...的反面...尝试排除最可能的条件

But, I'll join in the choir here to second the ideas mentioned of judicious use of the ternary operator if possible, identifying the most unlikely conditions which if met allow you to terminate the rest of the evaluation by excluding them first, and add ... the reverse of that ... trying to factor out the most probable conditions and states that can allow you to proceed without testing of the "fringe" cases.

Miriam的建议(上面)令人着迷,甚至很优雅,被称为概念艺术。 ;我实际上将尝试一下,试图搁置我的怀疑,即它会导致难以维护的代码。

The suggestion by Miriam (above) is fascinating, even elegant, as "conceptual art;" and I am actually going to try it out, trying to "bracket" my suspicion that it will lead to code that is harder to maintain.

我的务实方面说在没有相当具体的代码示例的情况下,这里没有一刀切的答案,并且没有完整描述条件及其相互作用。

My pragmatic side says there is no "one size fits all" answer here in the absence of a pretty specific code example, and complete description of the conditions and their interactions.

我是标志设置:意味着我的应用程序每次进入一些不太常见的模式或状态时,我都会设置一个布尔标志(对于该类甚至可能是静态的):对我来说,它简化了编写复杂的if / then运算,之后再进行评估。

I'm a fan of "flag setting" : meaning anytime my application goes into some less common "mode" or "state" I set a boolean flag (which might even be static for the class) : for me that simplifies writing complex if/then else evaluations later on.

最好,比尔

这篇关于C#中简化丑陋嵌套if-else树的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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