Closed. This question is opinion-based。它当前不接受答案。
想改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。
6年前关闭。
这两个功能是否有首选标准?像这样,在这样的函数中,是否应始终在“ if”之后加上“ else”?
编辑:例如,我看过等于以下定义的等式吗?你们中有人说您将删除“ else”吗?
想改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。
6年前关闭。
public int example1(int a, int b) {
if (a > b) {
return true;
} else {
return false;
}
}
public int example2(int a, int b) {
if (a > b) {
return true;
}
return false;
}
这两个功能是否有首选标准?像这样,在这样的函数中,是否应始终在“ if”之后加上“ else”?
编辑:例如,我看过等于以下定义的等式吗?你们中有人说您将删除“ else”吗?
public boolean equals(Object otherObject)
{
if (otherObject == null)
return false;
else if (getClass() != otherObject.getClass())
return false;
else
{
MyClass otherMyClass = (MyClass) otherObject;
return (variable.equals(otherMyClass.variable));
}
}
最佳答案
确实没有一个公认的标准。
例如,在这种情况下,流行的代码改进工具ReSharper会提示您“删除多余的else”块,因为您可以轻松编写它而无需其他代码。
但是相反地,有些人会说使用else块更具可读性。
对于您提供的示例,我想说的最简单的选择是
return (a > b);