【发布时间】:2021-03-05 16:10:12
【问题描述】:
我让作者阻止了这个 if 语句并对其进行了简化。如果嵌套返回相同的值,是否有更有效的方法来实现这一点?我可能只是想念它。
if (string == "something")
{
if (object1 == null || object2 == null || object3 == null)
{
return state1;
}
}
if (attribute1 || attribute2 || attribute3)
{
return state1;
}
return state 2;
【问题讨论】:
-
你能解释一下你想让这段代码做什么吗?
-
嵌套可以和
&&组合,第二块可以和||组合。 -
只需将所有
||条件合并到一行if(string == "something" && (object1 == null || object2 == null || object3 == null) || attribute1 || attribute2 || attribute3) -
结合条件即可。
if (string == "something") &&(object1 == null || object2 == null || object3 == null) || (attribute 1 || attribute2...等 -
我知道很多人不喜欢嵌套,但是太长的“if”条件比嵌套的 if 更难阅读。
标签: c# if-statement refactoring simplify