我正在为我正在工作的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() {}诸若此类。
发布于 2013-03-28 05:35:53
独立的功能坚持单一责任原则,而不是一个试图做很多事情的功能。这意味着您将拥有更小的函数,更易于理解、调试、修改和测试。在几乎所有的情况下,使用多个特定的函数要比使用一个单一的函数更好。
发布于 2013-03-28 05:38:40
这取决于这些函数内部发生了什么。如果大多数业务逻辑是相同的,只是输入参数不同(比方说,您需要准备不同的参数,但之后的逻辑是相同的),那么我会使用单个函数。在其他情况下,我会做多个更小的函数--它更容易维护,也更容易理解那里发生了什么。
https://stackoverflow.com/questions/15669926
复制相似问题