api-design

What are the downsides of passing function arguments as named values in a hash table/dictionary?


I am building a multi-language API to query key value stores and I am debating (with myself) about whether to use fixed argument lists or dynamic argument lists using hash tables.

In general I do realise that static type checking is not possible when using hashes, but as long as there are unit tests this is not a problem. But are there any other disadvantages?

For instance, using arguments:

set(Key,Value)

:and using hashes:

set(key: Key, value: Value)

Update: I have since read this: There are some instances where keyword communication is inferior to positional. The most obvious is the call to a procedure that is defined having only a single parameter. The keyword is, of course, unnecessary. Generally, keywording without defaults will require more keystrokes; keywording with defaults will require less. For some of today's machines, the processing of keyword sequences will be somwhat more expensive than the processing of positional sequences. The most disturbing disadvantage may be that an unintantional omission of a parameter on the programmer's part might lead to a program executing incorrectly, but without noticeable errors, since the default value for the parameter would be used.


Solution

  • Using PHP, you'd have something like those for your function : first, using arguments :

    function my_function($username, $password) {
        // Work with $username and $password
    }
    

    And using "hash values", you'd use an array :

    function my_function($params) {
        // Work with $params['username'] and $params['password']
    }
    

    (And the idea is the same in other languages -- such as Javascript, for instance)


    About hash values, a couple of great things :

    And at leat one very bad thing :


    About named parameters, a couple of great things :

    And for bad things :


    Now, generally speaking :