mardi 20 juin 2017

Which code is more readable for classifying four types?

I am developing iOS app with React Native. This code is for classifying iPhone by screen dimension.

Which code is more readable?

My co-worker told me the first code with ternary operator looks disaster.. How about you?


1. Ternary operator

const DEVICE_TYPE = (WIDTH >= 414) ? 4
                  : (WIDTH >= 375) ? 3
                  : (HEIGHT > 480) ? 2
                                   : 1


2. If-else

const DEVICE_TYPE = 4;

if (375 <= WIDTH && WIDTH < 414) {
    DEVICE_TYPE = 3;
} else if (HEIGHT > 480) {
    DEVICE_TYPE = 2;
} else {
    DEVICE_TYPE = 1;
}

Aucun commentaire:

Enregistrer un commentaire