假设我有这个功能:
inline bool fileExists(const char *name) {
FILE *file;
if (fopen_s(&file, name, "r") == 0)
{
fclose(file);
return true;
} else {
return false;
}
}
做上面那个更好,还是这个更好?
inline bool fileExists(const char *name) {
FILE *file;
if (fopen_s(&file, name, "r") == 0)
{
fclose(file);
return true;
}
return false;
}
编译时有区别吗?
最佳答案
这主要是见仁见智的问题。 IMO 第二种形式更好,因为它总是提供一个返回值。如果您修改函数以在 else 子句中做一些不同的事情,您可能会忘记添加返回值。在上面的示例中,生成的代码是相同的。
关于c++ - 带有返回值的 if 语句代码样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17167268/