dsft8327 2013-05-10 12:58
浏览 26

PHP代码重复。 什么时候复制代码是正确的方法?

I am using CodeIgniter but this question applies in a general sense too.

I have a table of transactions with columns

item_name | type | date | price | document

I want to do the following in two completely independent cases.

1) Get a list of transactions within a certain date range. 2) Get the total price of each transaction.type within a certain date range.

The former can be achieved by simply using a select statement with > datetimestamp

The latter can be achieved by selecting the SUM, and grouping by the type whilst like implementing any required where conditionals e.g with > datetimestamp

Although a simple case, to achieve this I need to have two methods however the bulk of both of these methods (namely the WHERE clauses) are duplicated across both methods.

In terms of speed etc it does not matter but it seems like pointless code reproduction.


A second example is as follows.

I previously had a method get_data($ID) which would get a row from a table based on the ID passed in.

As such in a separate method I would get my 100 items for example.. return an array, loop through them and call get_data for each.

This setup meant that many different methods could get different lists from different sources and then still use the same get_data function and a loop to get the required data.

This minimized code duplication but was incredibly ineffiecient as it meant looping through loads of data items and hundreds of db queries.

In my current setup i just join the data table in each of my methods - code duplication but clear improved efficiency.


A final example is as follows

In codeigniter I can have a function such as the following:

get_thing($ID)
{
$this->load->database();
$this->db->where('ID',$ID);
$this->db->get('table');
}

BUT in alternate situations i might want to only get items in a specific folder.. as such making the function more generic works better.. e.g.

get_thing($array)
{
$this->load->database();
$this->db->where($array);
$this->db->get('table');
}

but then I might want to use this function in two different contexts e.g a user page and an admin page whereby admins can see all items, even unverified ones. My code now becomes:

get_thing($array,$show_unverified = false)
{
$this->load->database();
$this->db->where($array);
if($show_unverified == false)
{
$this->db->where('verified','YES');
}
$this->db->get('table');
}

As you can probably see this can quickly get out of hand and methods can become overly complex, confusing and full of conditionals.


My question is as follows - What are best practices for minimizing code duplication, and how could they be applied to the above situations? I spent hours and hours trying to make my code more efficient yet I'm getting nowhere because I cant workout what I should really be trying to achieve.

Cheers

  • 写回答

3条回答 默认 最新

  • donglv6747 2013-05-10 13:06
    关注

    My idea on code duplication in database access functions is that it is often better to keep it separate.

    My rule here is especially that a function should not return different kinds of data depending on the parameter, for example it should not return a single user sometimes and other times an array of users. It may return error codes (false) though.

    It is ok though if the function implements different access levels, which are shared across several pages.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)