C#代码可读性问题

时间:2011-05-29 00:45:20

标签: c# readability

        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));

我应该如何编写该代码以使其更具可读性?

我想到了构建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;
    }
}

5 个答案:

答案 0 :(得分: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();

答案 1 :(得分:4)

这不是你需要的,但我可以使用Extension方法实现这种链方法,当条件可以解析为Or或者And的列表时。

这样的东西
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;
}

另一种可行的方法虽然可能不是很优,但是可以在.net中使用Predicate委托,并定义执行各个逻辑单元的方法列表。然后,您可以使用lambda替换嵌套的第三运算符。 我没有这个代码示例,抱歉。

最后,有时候没有什么比一个好的旧的switch语句更好了。我相信.Net倾向于将它们编译为跳转表,因此只要您首先通过最可分割的方式排列测试,那么您实际上可以获得性能相当且可读的代码。它是可维护的,而不是用技巧隐藏逻辑或实现。

答案 2 :(得分:0)

我认为真正的问题是你正在硬编码“配置数据”。如果您在哪里说,将这些“设置”删掉并将它们放入XML配置文件中,那么整个混乱是否会消失?

这也可能看起来有点矫枉过正,直到你来调整你的各种配置,让游戏更具可玩性......一个单独的配置FILE允许你轻松地玩(并恢复)。


修改

顺便说一句,我会将嵌套的三元语句格式化如下......以使其更具可读性。

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 :(得分: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;
    }
}

我确实建议尽可能在每个类中移动此计算,并覆盖不同的分支。

答案 4 :(得分: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;
    }
}  

..给出if的含义。做一个不同的IMO。