Removing Nesting By Returning Early

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;
}



---摘自<The art of readable code>

你可能感兴趣的:(Removing Nesting By Returning Early)