Are their any objective guiding priciples to choose between named fields over compactness?
const replacements = [
{ from: 'apple', to: 'manzana' },
{ from: 'banana', to: 'plátano' },
];
function replace(str, replacements) {
for (const {from, to} of replacements) {
str = str.replace(from, to);
}
return str;
}
vs
const replacements = [
[ 'apple', 'manzana' ],
[ 'banana', 'plátano' ],
];
function replace(str, replacements) {
for (rep of replacements) {
str = str.replace(...rep);
}
return str;
}
I think I prefer the first example. It feels documented to me and clearer. The second example is not clear to me, especially if the input data is separated from the function. I mean if I just see it in some library as in
const newStr = replace(str, [
[ 'apple', 'manzana' ],
[ 'banana', 'plátano' ],
]);
seems far less clear than
const newStr = replace(str, [
{ from: 'apple', to: 'manzana' },
{ from: 'banana', to: 'plátano' },
]);
Is there any principle to decide which or is it just entirely personal preference?
Sometimes there are criteria. For exmaple if the data is 100000 entries large maybe compactness outweighs any other criteria. In fact in that case you could argue for a single dimentional array to get even more compact (and even less clear)
const newStr = replace(str, [
'apple', 'manzana',
'banana', 'plátano',
]);
which is only slightly less incomprehensible than
const newStr = replace(str, ['apple', 'manzana', 'banana', 'plátano']);
But short of memory issues are there any other criteria for which to judge which style to use? There are a lot of occasions where using the spread operator suggests compactness but seems to make the code cryptic.