0

我正在为我正在处理的 MVC 项目开发模型。我想知道是否最好为一项任务提供多个功能,或者为每种可以处理的方式提供一个功能。例如,最好有类似的东西:

public function get($identifiers = null, $limit = null, $offset = null)
{
    if ($identifiers != null) {
        if (is_array($identifiers)) {
            $this->db->where($identifiers);
        } else {
            $this->db->where($this->_key, $identifiers);
            $method = 'row'.($this->_return_array ? '_array' : '');
            return $this->db->get($this->_table)->$method();
        }
    }

    if ($limit != null) {
        $this->db->limit($limit, $offset || null);
    }

    if (!count($this->db->ar_orderby)) {
        $this->db->order_by($this->_order);
    }

    $method = 'result'.($this->_return_array ? '_array' : '');
    return $this->db->get($this->_table)->$method();
}

处理多种情况或具有单独的功能,例如

get($id) {}
get_where($where) {}
get_all() {}

等等。

4

2 回答 2

1

单独的功能遵循单一职责原则,比一个尝试做很多事情的功能更接近。这意味着您将拥有更易于理解、调试、修改和测试的更小的函数。在几乎所有情况下,您最好使用多个特定功能而不是一个单一功能。

于 2013-03-27T21:35:52.550 回答
0

这取决于这些函数内部发生了什么。如果大部分业务逻辑都是一样的,只是输入参数不同(比如说,你需要准备不同的参数,但之后逻辑是一样的),那么我会选择单个函数。在其他情况下,我会做多个较小的功能 - 它更易于维护,更容易理解那里发生的事情。

于 2013-03-27T21:38:39.840 回答