重构我的 if 语句代码
我已经搞乱这段代码一个多小时了,试图以不同的方式重新排列它。有没有更简单的写法?
if x is not Number ;// if x is string
{
if y is not Number ;// x, y both strings
{
Eval(x)
Eval(y)
return
}
else ;// x is string, y is Number
{
Eval(x)
Scale(y)
return
}
}
else if y is not Number ;// x is Number, y is string
{
Scale(x)
Eval(y)
return
}
else ;// both are numbers
{
Scale(x)
Scale(y)
return
}
I've been messing with this bit of code for over an hour trying to rearrange it different ways. Is there any easier way to write it?
if x is not Number ;// if x is string
{
if y is not Number ;// x, y both strings
{
Eval(x)
Eval(y)
return
}
else ;// x is string, y is Number
{
Eval(x)
Scale(y)
return
}
}
else if y is not Number ;// x is Number, y is string
{
Scale(x)
Eval(y)
return
}
else ;// both are numbers
{
Scale(x)
Scale(y)
return
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您想要
Eval
字符串和Scale
数字。不要有四个显式情况(三个变量会变成八个情况),而是独立处理x
和y
的每个情况:或者,更好的是,您可以推送
Eval
/Scale
为实用方法:...然后使用它...
如果您选择好的方法名称,那么创建实用方法可以使代码更具可读性并帮助您避免复制粘贴重复。
It looks like you want to
Eval
strings andScale
numbers. Instead of having four explicit cases (which would become eight with three variables), handle each case forx
andy
independently:Or, better yet, you can push
Eval
/Scale
into a utility method:...and then use it...
If you pick good method names, then creating a utility method makes the code more readable and helps you avoid copy-and-paste repetition.