开发者

Is it better to use arrays instead of parameter lists when designing an api for an MVC application?

When writing a CRUD MVC application, would you s开发者_C百科uggest using arrays instead of long (even short) parameter lists when writing an api for your business layer (model)?

For instance, which would you suggest:

1

// Posts::getPosts(20, 0, $category, 'date_added');
static function getPosts($limit = NULL, $offset = NULL, Model_Category $category = NULL, $sort_by = NULL);

2

// Posts::getPosts(array('limit' => 20, 'offset' => 0, 'category' => $category, 'sort_by' => 'date_added'));
static function getPosts(array $options = NULL);`

1 seems a lot cleaner and less prone to bugs, but 2 seems WAY more flexible (can easily add/switch parameters without changing the api). Just looking for reasons to go either way.

Thanks


With just an array, the guy who will try to call your method doesn't know which parameter(s) it expects.
And his IDE will not be able to help either...

=> He'll have to go read the documentation -- which takes time.


On the other hand, with the first solution, just looking at the declaration of the method (and my IDE does display that when I type the name of the method), I know what parameters it expects.


I agree that your second solution (array of named parameters) is way more flexible.

But, especially when there are only a few parameters, I tend to prefer the first one -- just for the reason I wrote.


I go with this rule of thumb:

If

  • there are more than 5 arguments
  • there is no logical order of the arguments
  • there is not a clear logical dependency between arguments
    and/or
  • most of the arguments are optional

Then it is probably a good idea to use an array to simulate keyword arguments. Otherwise, just go with standard arguments.

Also, consider using a parameter object to do complicated method calls.

EDIT: What would I do with this?

public static function search(
  $keywords,
  $limit = NULL,
  $offset = NULL,
  Model_Post_Type $type = NULL,
  Model_Category $category = NULL
)

Well, with a parameter array (also known as keyword arguments in languages that support them, like Python), my personal preference would be to do this:

public static function search($keywords, $options = array()) {
  $default_options = array(
    'limit' => NULL,
    'offset' => NULL,
    'post_type' =>  NULL,
    'category' => NULL
  );
  extract(array_merge($default_options,$options));
  // search logic, using $keywords, $limit, $offset, $post_type, $category
}

This gives you a few benefits:

  • Anything in $options is completely optional. Any required arguments should be arguments.
  • Gives you complete control over the defaults of those options, even allowing for complicated expressions in the array initializer.
  • Allows you to add new (optional) search options later on, while remaining backwards-compatible with existing code.
  • extract() makes key-value pairs into variable-value pairs, so the rest of the method is completely oblivious to the fact you're using a parameter array and not normal arguments.


That probably depends on what you need. Do the parameters you use have a logical order? Is one inapplicable without another? It's not really a one-or-the-other-for-everything.

Because of the nature of this case, separate arguments are probably the best route. One thing I would suggest however is that it provide more useful defaults.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜