vendredi 19 mars 2021

Is t a good practice to use continue keyword to get rid of if else ladders in loops?

It is usually recommended to avoid if else ladders for better readability.

So I was wondering, say in a situation where I have an if else ladder like this,

while (someFunc(ptr)) {
    if (ptr.equals(ptrA)){
        //update ptr
    }
    else if(ptr.equals(ptrC)){
        //update ptr
    }
    else {
        //update ptr
    }
}

Would it be a good practice to do it like this instead?

while (someFunc(ptr)) {
    if (ptr.equals(ptrA)){
        //update ptr
       continue;
    }
    if(ptr.equals(ptrC)){
        //update ptr
        continue;
    }
    //update ptr
}

Aucun commentaire:

Enregistrer un commentaire