vendredi 17 février 2017

Efficiency vs readability of if, else if, else statements

Not exactly how to word the title but the general question is comparing these two blocks of code. The first is the original and the second is the one that would replace it.

Does this actually improve the code, it's efficiency or would it only improve readability? I don't believe they are functionally different, unless I am missing something.

1ST:

    if(stl1YVal < stl2YVal){
        return -1;
    }else if(stl1YVal > stl2YVal){
        return 1;
    }else{
        if(stl1XVal < stl2XVal){
            return -1;
        }else if(stl1XVal > stl2XVal){
            return 1;
        }else{
            return 0;
        }
    }

2ND:

    if(stl1YVal < stl2YVal || stl1XVal < stl2XVal){
        return -1;
    }else if(stl1YVal > stl2YVal || stl1XVal > stl2XVal){
        return 1;
    }else {
        return 0;
    }

Aucun commentaire:

Enregistrer un commentaire