条款26:尽可能延后变量定义式的出现时间
过早定义变量
std::string encryptPassword(const std::string& password) {
std::string encrypted;
if (password.length() < minimumPasswordLength) {
throw logic_error("Password is too short");
}
...
return encrypted;
}延后变量定义,直到需要它
std::string encryptPassword(const std::string& password) {
if (password.length() < minimumPasswordLength) {
throw logic_error("Password is too short");
}
std::string encrypted;
...
return encrypted;
}跳过无意义的 default 构造
关于循环
Last updated