【发布时间】:2011-05-29 00:45:19
【问题描述】:
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;
}
}
【问题讨论】:
-
给我看等价的
if-then语法 - 这让我的眼睛受伤...... -
这些标量值会在应用执行期间发生变化,还是对于每种生物类型都是永久性的?
-
当生物是 SpecialCreature 并且是 IGreatCreature 或不是 IGreatCreature 时,计数相同。
-
我想知道为什么这被否决了?
标签: c# readability