Removing Nesting by returning early(編寫易讀代碼)
1:
if (user_result == SUCCESS) {
if (permission_result != SUCCESS) {
reply.WriteErrors("error reading permission");
reply.Done();
return;
}
reply.WriteErrors("");
}else {
reply.WriteErrors(user_result)
}
reply.Done();
use return
if (user_result != SUCCESS) {
reply.WriteErrors(user_result);
reply.Done();
return;
}
if (permission_result != SUCCESS) {
reply.WriteErrors("error reading permission");
reply.Done()
return;
}
reply.WriteErrors("");
reply.Done();
2:
for (int i=0;i<results.size();i++){
if (result[i] != NULL){
non_null_count++;
if (result[i]->name != ""){
count << "Considering candidate..." << endl;
}
}
}
use continue inside loops
for (int i=0;i<results.size();i++){
if (result[i] == NULL) continue;
non_null_count++;
if (result[i]->name == "") continue;
cout << "Considering candidate..." << endl;
}