简单原则(Keep It Simple and Stupid)
例子1
- 猜疑返回值,不够明确
if (system("ls")) {
// do something
}
- 明确做法
if (system("ls") != 0) {
// do something
}
例子2
- 判断语句有逻辑语句,不够直观
if (system(i > 0 ? "ls" : "ls -la") != 0) {
// do something
}
- 直观做法
const char *cmdLine = i > 0 ? "ls" : "ls -la";
if (system(cmdLine) != 0) {
// do something
}