2011-05-29 42 views
4
 int scalar = creature is SpecialCreature ? (creature.IsAwesome ? 700 : 500) : (creature is NotSoNormalCreature ? 
      (creature.IsAwesome ? (creature is IGreatCreature ? 450 : 280) : 240) : 
      (creature.IsAwesome ? (creature is IGreatCreature ? 300 : 200) : 160)); 

我该如何编写代码以使其更具可读性?C#代码可读性问题

我以为只是建立IFS的,但后来我想到了做某种“ConditionFactory”怎么样?这是否有意义,还是这样简单的任务太复杂了?

int scalar; 

if (creature is SpecialCreature) 
{ 
    scalar = creature.IsAwesome ? 700 : 500; 
} 
else if (creature is NotSoNormalCreature) 
{ 
    if (creature.IsAwesome) 
    { 
     scalar = creature is IGreatCreature ? 450 : 280; 
    } 
    else 
    { 
     scalar = 240; 
    } 
} 
else 
{ 
    if (creature.IsAwesome) 
    { 
     scalar = creature is IGreatCreature ? 300 : 200; 
    } 
    else 
    { 
     scalar = 160; 
    } 
} 
+7

让我看看等价的if-then语法 - 这会让我的眼睛受伤... – IAbstract 2011-05-29 00:50:28

+0

这些标量值在应用程序执行期间是否改变,或者它们对于每种生物类型都是永久的? – twk 2011-05-29 00:54:19

+0

当生物为SpecialCreature并且IGreatCreature或不IGreatCreature时,它计数相同。 – twk 2011-05-29 01:01:41

回答

8

不知道你去为完全的东西,但因为你使用的是基本类型继承链,您可能选择这样做

interface ICreature 
{ 
    bool IsAwesome { get; set; } 
    int GetScalar(); 
} 

abstract class Creature : ICreature 
{ 
    public bool IsAwesome { get; set; } 
    public virtual int GetScalar() 
    { 
     return 160; 
    } 
} 

class SpecialCreature : Creature 
{ 
    public override int GetScalar() 
    { 
     return this.IsAwesome ? 700 : 500; 
    } 
} 

class NotSoNormalCreature : Creature 
{ 
    public override int GetScalar() 
    { 
     return this.IsAwesome ? 450 : 280; 
    } 
} 

// more ICreatures... 

这将让你有生物实现自己逻辑确定的标量,你的消费代码可以失去照顾的并发症。

ICreature creature = GetCreatureFromSomewhere(); 
int scalar = creature.GetScalar(); 
+0

我喜欢你的想法,但是,这个标量是用来计算一个特定的下降的机会,并且在整个地方使用公式的想法会使它更难追踪或改变未来 – bevacqua 2011-05-29 01:15:15

+1

+1做得好, 容易明白。 – twk 2011-05-29 01:25:30

+0

@尼科,抱歉,您觉得它不能解决您的具体需求。然而,对于某些处于类似情况的人来说这可能是有益的,如果不在这里,您可能会发现这样的模式对别处有用。 – 2011-05-29 01:28:35

0

我认为真正的问题是,您正在对“配置数据”进行硬编码。如果你在哪里说,把这些“设置”翻出来放到一个XML配置文件中,那么这个混乱不会消失?

这也可能看起来像是矫枉过正,直到你来调整你的各种配置,使游戏更具可玩性......一个单独的配置文件允许你轻松地播放(和恢复)。


编辑:

顺便说一句,我就格式化为低于嵌套terniary声明......,以使其更具可读性。

int scalar = 
    creature is SpecialCreature 
    ? creature.IsAwesome ? 700 : 500 
    : creature is NotSoNormalCreature 
    ? creature.IsAwesome 
     ? creature is IGreatCreature ? 450 : 280 
     : 240 
    : creature.IsAwesome 
     ? creature is IGreatCreature ? 300 : 200 
     : 160 
; 

干杯。基思。

+3

-1:它不能真正解决问题 – twk 2011-05-29 01:04:11

+0

为什么我会使用xml?这些都是静态的,只是物品掉落的公式,而不是绑定到生物上的配置值。 -1 – bevacqua 2011-05-29 01:05:10

4

这不是很你需要什么在这里,但我使用扩展方法来实现这种链法时的条件就可以解决到Or的或与公司的名单。

喜欢的东西

if (true.IfOr(condition1 == a, condition2 == b) 
{ 
    something(); 
} 

扩展方法则很简单:

public static bool IfOr(this bool result, params bool[] tests) 
{ 
    foreach (bool test in tests) 
    if (!test) 
     return !result; 
    return result; 
} 

,可以工作,虽然它可能不是非常最佳的另一种方法是使用在谓词委托。网络并定义执行您的个人逻辑单元的方法列表。然后,您可以用lambda替换嵌套的第三级操作符。 我没有这个代码示例,虽然手,抱歉。

最后虽然有时只是没有什么比一个很好的旧switch语句更好。我认为,净趋于编译这些在跳转表所以只要你通过最整除的人安排你的测试,然后再实际上你可以得到相当高性能和可读的代码。它是可维护的,而不是用技巧来隐藏逻辑或实现。

0

这是怎么了我重新做了代码,并使其可读

// Original code spread apart 
int scalar = creature is SpecialCreature ? (
    creature.IsAwesome ? 700 : 500 
) : (
    creature is NotSoNormalCreature ? (
     creature.IsAwesome ? (
      creature is IGreatCreature ? 450 : 280 
     ) : 240 
    ) : (
     creature.IsAwesome ? (
      creature is IGreatCreature ? 300 : 200 
     ) : 160 
    ) 
); 

// Readable code with hybrid if() and ? : 
if (creature is SpecialCreature) 
{ 
    scalar = creature.IsAwesome ? 700 : 500; 
} 
else if (creature is NotSoNormalCreature) 
{ 
    if (creature.IsAwesome) 
    { 
     scalar = creature is IGreatCreature ? 450 : 280; 
    } 
    else 
    { 
     scalar = 240; 
    } 
} 
else 
{ 
    if (creature.IsAwesome) 
    { 
     scalar = creature is IGreatCreature ? 300 : 200; 
    } 
    else 
    { 
     scalar = 160; 
    } 
} 

我想推荐每个类中移动这样计算如果可能,覆盖了不同的分支。

+1

你刚才编辑的问题看起来就像我的答案..真棒! – ja72 2011-05-29 01:25:40

+0

我还没有看到你的答案 – bevacqua 2011-05-29 01:32:03

0

如何历久弥新:

if (creature is SpecialCreature) 
{ 
    scalar=getSpecialCreatureScalar(creature); 
} 
else if (creature is NotSoNormalCreature) 
{ 
    scalar=getNotSoNormalCreatureScalar(creature); 
} 
else 
{ 
    scalar=getScalar(creature); 
} 

..和再

int GetSpecialCreatureScalar(SpecialCreature creature) 
{ 
    return creature.IsAwesome ? 700 : 500; 
} 

int GetNotSoNormalCreatureScalar(NotSoNormalCreature creature) 
{ 
    if (creature.IsAwesome) 
    { 
     return creature is IGreatCreature ? 450 : 280; 
    } 
    else 
    { 
     return 240; 
    } 
} 

int GetScalar(Creature creature) 
{ 
    if (creature.IsAwesome) 
    { 
     return creature is IGreatCreature ? 300 : 200; 
    } 
    else 
    { 
     return 160; 
    } 
} 

..Gives的,如果是一个意思。使不同的国际海事组织。

+0

你为什么要这样的功能?我会把第一个字母大写。 – bevacqua 2011-05-29 02:02:52