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