mardi 4 août 2020

Refactor if-else control flow

I came across the following snippet while going through the code:

if(a || b){
    if(a) {
        doSomething();
    }
    doSomethingElse();
} else {
    throw new Exception("blah");
}

I was wondering how can I refactor this code for better readability (or it's already in optimal shape?). Below is my first attempt:

if(!a && !b){
    throw new Exception("blah");
}
if(a){
    doSomething();
}
doSomethingElse();

Does this look better?

Aucun commentaire:

Enregistrer un commentaire