Hello. I put a question on stackoverflow javascript best practices, http://stackoverflow.com/questions/33676880/on-javascript-code-where-when-how-check-function-arguments-best-practices . But I affraid that developers there don´t underestand me, my fall of course.
Well! I ask here cause I belong to here, Meteor, where I read many post of “on the trench” developers like @SkinnyGeek1010, @awatson1978, @arunoda, …
I had edited my code before put on stackoverflow cause is was a javascript questions but here I have the freedom to speak the Meteor way.
I have this functions:
// This is a private function, don´t check for undefined arguments.
function _arrayKeyDiff(a1, a2) {
let r = {s:[], n: [], l: []};
for(let k of a1) if (a2.indexOf(k) === -1) r.l.push(k); else r.s.push(k);
for(let k of a2) if (a1.indexOf(k) === -1) r.n.push(k);
return r;
}
// This a public fuction, check for isArray arguments.
arrayKeyDiff = function arrayKeyDiff(a1, a2) {
return Array.isArray(a1) && Array.isArray(a2) && _arrayKeyDiff(a1, a2);
}
and this function that use public arrayKeyDiff
findDuplicateKey = function findDuplicateKey(...o) {
// here 'o[0]' is checked cause Object.keys throw exception on undefined values
if (!o[0]) return;
let keys = [], acc = Object.keys(o[0]);
for(let i = 1; i < o.length; i++) {
let r;
// 'o[i]' here again check before call Object.keys
if (o[i]) {
r = arrayKeyDiff(acc, Object.keys(o[i]));
acc = acc.concat(r.n);
r = r.s;
}
keys.push(r);
}
return keys;
}
and a ‘boolean’ return function that use ‘findDuplicateKey’ and console log mensage errors.
hasDuplicateKeyError = function hasDuplicateKeyError(...o) {
let error = false;
let r = findDuplicateKey(...o);
r.forEach((v,i) => {
if (v && v.length) {
error = true;
console.log('obj:', i, ' has duplicate key:', v);
}
});
return error;
}
finally this function, ‘hasDuplicateKeyError’ is used on Match.Where
check pattern like this:
nonHasDuplicateKeyObject = Match.Where(function(x) {
check(x, [Match.Any]);
return !hasDuplicateKeyError(...x);
});
// test code
const TestObj1 = { a: 1, b:1}, TestObj2 = { a:1, c:2 };
check([undefined, TestObj1, TestObj2], nonHasDuplicateKeyObject);
nonHasDuplicateKeyObject have a problem. If my first argument is undefined the call to ‘findDuplicateKey’ on function hasDuplicateKeyError, throw a exception. I refer to this line:
hasDuplicateKeyError = function hasDuplicateKeyError(...o) {
let error = false;
let r = findDuplicateKey(...o);
r.forEach((v,i) => {
Here var ‘r’ is undefined cause this line on ‘findDuplicateKey’ function
findDuplicateKey = function findDuplicateKey(...o) {
// here 'o[0]' is checked cause Object.keys throw exception on undefined values
if (!o[0]) return;
Then I must be guard ‘r’ for ‘r.forEach’ cause it throw a exception is ‘r’ is undefined, then it need a ‘if’ like …
> hasDuplicateKeyError = function hasDuplicateKeyError(...o) {
> let error = false;
> let r = findDuplicateKey(...o);
> if (r) r.forEach((v,i) => {
Well my problem is with ‘guards’. I start to put it all over the places, on any function before pass arguments to others functions. I lost to much time thinking on where put it and what check.
For example in function:
findDuplicateKey = function findDuplicateKey(...o) {
// here 'o[0]' is checked cause Object.keys throw exception on undefined values
if (!o[0]) return;
let keys = [], acc = Object.keys(o[0]);
I have been thinking on replace
if (!o[0]) return;
let keys = [], acc = Object.keys(o[0]);
with
let o1 = o[0] || []; // default value []
let keys = [], acc = Object.keys(o1]);
And let the code continue on ‘undefined’ arguments passed but I don´t sure is this a good practice or bad or …
I think I miss something here, a good advice, a good pratice a, Lama mantra , …
Sorry for the huge wall text