jeudi 16 février 2017

Is one IF with mulitple booleans suggested over mulitple IFs with one boolean each?

I am discussing with someone why writing code like this first example could be better or worse than the following code

if(a == 1 || b == 2 || c == 3 || ...){}

and so on for a few more boolean statements. I suggested that using if and if else could be cleaner and more efficient like the following code, especially when debugging if your statement grew large enough to cause issues.

if(a == 1){}
else if(b == 2){}
else if(c == 3){}

His remark was that it would have extra garbage code because for one if (top example) I would have a few lines that would do X where as the second example I would have to include X in every if assuming they were all meant to do the same thing.

I would like to know are there any trade offs besides possible debugging issues or just preference on the way of coding? The performance should be roughly the same I believe.

Aucun commentaire:

Enregistrer un commentaire