大函数分成小函数集合的优缺点

来源:互联网 发布:上海数据交易中心 coo 编辑:程序博客网 时间:2024/06/02 13:16
在今天的编程中,我需要改进我的计算仓储费代码,本来原来写的可以用,但是甲方改了需求之后我的代码就需要改动。再回头去看我的代码,我竟然一点都不想看。(代码的可读性不强)——因为我的函数太长了,并且两个大函数中有大部分都是在做同样的事。于是我决定重构一下代码
//客户单车算费public function Count_Onecar_Storage($car_id,$lastcount,$end){$listin=array();$carcourage=array();$listin=$this->Getcarinfo($car_id,$lastcount);for ($i=0; $i <count($listin)-1 ; $i++) { $days = ($listin[$i+1]['time'] - $listin[$i]['time']) / 86400;$result[$i]['storage_fee'] += (floor($days) + 1) * $this -> price($company_id, $listin[$i]['store_id']);$result[$i]['time_start'] = date('Y-m-d', $listin[$i]['time']);$result[$i]['time_end'] = date('Y-m-d', $listin[$i+1]['time']);$result[$i]['time'] = $result[$i]['time_start'].'——'.$result[$i]['time_end'];$result[$i]['store_name'] = $this->getstorename($listin[$i]['store_id']);}$days = ($end - $listin[$i]['time']) / 86400;$result[$i]['storage_fee'] += (floor($days) + 1) * $this -> price($company_id, $listin[$i]['store_id']);$result[$i]['time_start'] = date('Y-m-d', $listin[$i]['time']);$result[$i]['time_end'] = date('Y-m-d', $end);$result[$i]['time'] = $result[$i]['time_start'].'——'.$end;$result[$i]['store_name'] = $this->getstorename($listin[$i]['store_id']);return $result;}//得到单车可用信息public function Getcarinfo($car_id,$lastcount){$listin= array();$result=array();$Car_Outin_Store = M('car_outin_store');//得到所有入库的时间$conditionin['car_id'] = $car_id;$conditionin['out_in_type'] = 1;$listin = $Car_Outin_Store -> where($conditionin) -> select();for ($i=0; $i <count($listin) ; $i++) { if($listin[$i]['time']>=$lastcount){break;}}$k=1;if($i==0){$result[0]['time']=$listin[$i]['time'];$result[0]['location_id']=$listin[$i]['location_id'];}else{$result[0]['time']=$lastcount;$result[0]['location_id']=$listin[$i-1]['location_id'];}for(;$i<count($listin)-1;$i++){if($listin[$i]['location_id']!=$listin[$i+1]['location_id']){$result[$k]['time']=$listin[$i+1]['time'];$result[$k]['location_id']=$listin[$i+1]['location_id'];$k++;}}return $result;}最重要的是对于数据的整理。它让我的思路更加简单,把复杂的问题简单化,把规律不明显的事情变得有规律。
<span style="white-space:pre"></span>因为还没有调试所以只能当伪码看。在得到单车可用信息的时候,我得到了每次车入库的记录,但是可能第一个数据并不是我想要的规律。因为第一个数据可能是上次结费的时间。在有了第一条数据后,用后面的数据和前面的进行比较。location_id不同就录入数据。在最后的数据刚好能被处理掉。
<span style="white-space:pre"></span>php中的数据整理还是让人很头疼啊。
<span style="white-space:pre"></span>接下来就是重点了。
<span style="white-space:pre"></span>我原来写的时候并没有进行数据整理。于是在Count_onecar_Storage中我并不能有很好的思路。当某个问题比较难时可以假设自己已经解决它了,然后往下走。

0 0
原创粉丝点击