C#代码可读性问题
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;
}
}
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));
how should I write that code to make it more readable?
I thought of just building the ifs, but then I thought how about making some kind of "ConditionFactory"? Would that make any sense, or is it just too intricate for such a simple task?
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
完全不确定你要做什么,但由于你使用的是基类型继承链,你可能会选择做类似的事情,
这将允许你让生物实现自己的逻辑来确定标量,并且你的消费代码可以摆脱照顾的麻烦。
Not sure entirely what you're going for, but since you are using a base type inheritance chain, you might elect to do something like
Which would allow you to have the creature implement its own logic for determining the scalar, and your consuming code can lose the complication of caring.
这并不完全是您所需要的,但当条件可以解析为 Or 或 And 列表时,我使用扩展方法来实现这种链方法。
像
扩展方法这样的事情就非常简单了:
另一种可行的方法(尽管可能不是最佳方法)是利用 .net 中的 Predicate 委托并定义执行各个逻辑单元的方法列表。然后,您可以将嵌套的三级运算符替换为 lambda。
不过,抱歉,我手头没有这方面的代码示例。
最后,有时没有什么比一个好的旧 switch 语句更好的了。我相信 .Net 倾向于将它们编译为跳转表,因此只要您首先按最可整除的来安排测试,那么您实际上可以获得相当高性能和可读的代码。而且它是可维护的,而不是用技巧隐藏逻辑或实现。
It's not quite what you need here but I use an Extension method to implement this sort of chain method when the condition can be resolved to a list of Or's or And's.
Something like
the extension method is then quite easy:
Another method that can work although it may not be very optimal is to make use of the Predicate delegate in .net and define a list of methods that perform your individual units of logic. You can then replace your nested tertiary operators with a lambda.
I don't have the code sample of this to hand though, sorry.
Lastly though, sometimes there is just nothing better than a good old switch statement. I believe that .Net tends to compile these as jump tables so as long as you arrange your test by the most divisible ones first, then you can actually get quite performant and readable code. And it's maintainable instead of hiding logic or implementation with tricks.
我认为真正的问题是您正在对“配置数据”进行硬编码。如果你想说的是,将这些“设置”撕掉并将它们放入 XML 配置文件中,那么整个混乱状态不会消失吗?
这也可能看起来有点矫枉过正,直到你调整你的各种配置,使游戏更具可玩性......一个单独的配置文件可以让你轻松地玩(和恢复)。
编辑:
顺便说一句,我将按如下所示格式化嵌套三元语句......以使其更具可读性。
干杯。基思.
I think the real issue is that you're hardcoding "configuration data". If you where to say, rip those "settings" out and put them into an XML config file then does not that whole mess disappear?
That too may seem like overkill, until you come to tweak your various configs, to make the game more playable... a seperate config FILE allows you to play (and revert) easily.
EDIT:
By the way, I would format that nested terniary statement as below... to make it more readable.
Cheers. Keith.
这就是我重新编写代码并使其可读的方法,
我确实想建议如果可能的话,在每个类中移动此计算,并覆盖不同的分支。
This is how I re-did the code and made it readable
I do want to recommend moving this calculation inside each class if possible, with overrides for the different branches.
好老的怎么样:
..然后
..赋予 if 一个含义。创造了一个不同的IMO。
How about good old:
..And then
..Gives the if's a meaning. Makes a different IMO.