lundi 25 novembre 2019

Is it better to prepare variables before an if statement rather than using else?

Take this example block of code:

if ($flag) {
    $actionText = 'Text 1';
    $action = 'save';
} else {
    $actionText = 'Text 2';
    $action = 'delete';
}

My manager requested it be like this, to avoid an unnecessary else:

$actionText = 'Text 2';
$action = 'delete';

if ($flag) {
    $actionText = 'Text 1';
    $action = 'save';
}

I feel like 1) it's less performant (potentially setting the variables twice vs choosing either the if/else path; else doesn't require additional analysis to perform, does it?) and 2) it's less intuitive to look at and understand immediately.

Is this a paradigm I don't know about? Can anyone give some insight?

Aucun commentaire:

Enregistrer un commentaire