I'm wondering what are some common ways to approach the problem of figuring 
out which arguments the user has passed to a function.

It's a problem I've encountered many times when writing libraries with 
APIs; especially for functions with optional arguments.
Each such function requires logic to figure out which of the arguments were 
supplied, and assign default values to those which weren't.
A function with just a few arguments requires a large chunk of code just 
for this purpose.

An alternative approach is to use an `options` object, the fields of which 
represent the arguments, and directly assigned by the user.
This, however, imo, is less suitable for public APIs. I prefer to have 
function signatures with distinct arguments.

It seemed to me like there should be a programmatic solution to this. Each 
function's arguments should hold certain conditions. Arguments must be of 
certain types, some are optionals, some have default values, etc.
This led me to develop a module which allows to declare the properties of 
the function's arguments. The module resolves tnd disambiguates the 
arguments passed by the user based on those declarations.

https://github.com/EyalAr/Decree

Decree works by analyzing a list of argument declarations. It then figures 
out all the possible combinations.
For example, a function which receives two arguments, with the first one 
optional, has two possible combinations - the user can call the function 
with one argument (thus omitting the first one), or with two (thus 
providing the first one).
A function with two arguments, of which non is optional, has only one 
combination.
When the user calls the function, decree will match the supplied arguments 
to a certain combination. If no combination is found, the user supplied 
invalid arguments. If more than one combination is found, the user was not 
specific enough, and there is ambiguity. Decree can tell where is the 
problem.

How do you usually approach this problem?
And what do you think about this solution?

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/714e2782-1b8b-4203-bcef-36761b65d993%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to