As a functional programmer I regularly pass functions to higher order functions, to combine them in a specific way and create new behavior. Often HOFs serve as wrappers that – as a side effect – disguise the original properties of the given function. A curry function is a good example for that:
// any arbitrary curry function:
function curry(f) {
function curried() {
if (arguments.length < f.length) {
var args = Array.prototype.slice.call(arguments);
return function () {
return curried.apply(null, args.concat(Array.prototype.slice.call(arguments)));
}
} else {
return f.apply(null, arguments);
}
}
return curried;
}
function sum(x, y, z) {
return x + y + z;
}
// reflection in effect:
sum.name; // sum
sum.length; // 3
sum = curry(sum);
// application:
sum(1, 2, 3); // 6
sum(1)(2)(3); // 6
sum(1, 2)(3); // 6
// disguised reflection:
sum.name; // curried
sum.length; // 0
I wonder if this reflective ability should be preserved throughout the code, even though this may result in higher complexity. Or is it poor style to build dependencies against such properties anyway?
If you think that this question is mainly opinion based, just answer the following:
Are there use cases for length
and name
, which demonstrate the necessity to preserve them in the entire code base?