Name for defining a function with fewer arguments that calls the original function

26 Views Asked by At

I often have functions that take a bunch of arguments and then I define convenience wrappers that hardcode some of the arguments.

Dummy C example:

int add(int a, int b) { return a + b; }
int addFive(int a) { return add(a, 5); }

The technique shortens the outputted callsite object code and on ABIs that pass arguments through registers, the wrappers are fast because of tailcalls and compact as long as the hardcoded arguments are the final ones.

Does this technique have a name?

Currying and partial application seem to be related What is the difference between currying and partial application? but it seems that those require that the wrapper be returned, which only works in some languages (definitely not C, which is my main target).

1

There are 1 best solutions below

0
Lajos Arpad On BEST ANSWER

That's a well-known and oftentimes used programming technique. I have never seen a name for it, so if there is a name for this technique already, then please let me know and I will remove this answer.

So, if I'm correct and there is no specific name for this, then, to shortly and compactly reference to this technique linguistically, I would use some short description for it, such as:

"method parameter wrapper"

and if I was asked for the meaning to this, I would just say that by that I refer to the implementation of f2, with less parameters than f1, with f2 consisting only of calling f1 and hard-coding some parameters.

If this is for documentation, then you can choose the name you prefer, which may be the one above, or something else and then consistently use that term in the documentation, maybe in an abbreviated manner if it's used frequently.