5
Lately I’ve been too preoccupied with preventing code errors. Yet I was left with that doubt:
It is better to check (prevent) or remedy (Ensure existence)?
Let’s say you did :
$var = array(
'name' => 'Guilherme',
'lastName' => 'Lautert'
);
What is ideal to check if the index exists or ensure that it exists?
Verify
if(isset($var['name'])){
# code ...
}
Ensure
$default = array(
'name' => null,
'lastName' => null,
);
$var = array(
'name' => 'Guilherme',
);
$var = array_merge($default, $var);
$lastName = $var['lastName'];
Editing
As commented may be based on opinion, so assuming a situation :
jQuery.ajax({
url: APP + "/"+CONTROLADOR_ATUAL+"/jsonGetResposta",
data: { cd : cd},
dataType: 'json',
async: false,
success: function(msg){
jQuery('#vl_total').val(msg.dados.vlTotal);
jQuery('#nr_total').val(msg.dados.nrTotal);
}
});
Ideally, ensure that the index exists in PHP, or check whether dados
, vlTotal/nrTotal
, exists in the JS?
It depends on the case. The two codes are not equivalent, so it is difficult to compare them.
– Maniero
I voted to close because I think it’s based on opinions. There’s already an answer that starts with "particularly prefer...."
– Franchesco
Sorry for, particularly, I say this because following the usability standards should if whenever possible return a feedback to the user, but I do not know where you intend to employ the code. I think you should evaluate better before judging a question. Thank you.
– user3010128
$var += ['nome' => 'Wallace de Souza']
– Wallace Maxters
@bigown depending on the case, he can wear both.
– Wallace Maxters