写出更好看,更简洁的代码

时间:2012-12-03 00:18:54

标签: php

我已经获得了以下代码片段,它可以正常运行并完成我需要它做的事情。唯一的问题是我认为它有点'冗长',可以做一些优化。如果可能的话,有人可以帮助我减少重复性。大部分代码非常相似......

非常感谢

<?php 

// condition = Less Than; More Than; Between
// noofweeks = this is the number of weeks chosen by the user

function fmsSupplements($condition, $conditionWeeks, $noofweeks, $weekno, $weekStartno,      $weekEndno, $basicprice, $supplementAmnt, $supplementType) {

if ($condition== "Between") {
// I need to get the start and end values as the data in this parameter should look like 1-17 
$betweenArray = explode('-',$conditionWeeks);
$startWeek = $betweenArray[0];
$endWeek = $betweenArray[1];
}


if(($condition == "Less Than") && ($noofweeks < $conditionWeeks) && ($supplementType ==    'Subtract') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice -  $supplementAmnt; }

elseif(($condition == "Less Than") && ($noofweeks < $conditionWeeks) && ($supplementType == 'Add') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice + $supplementAmnt; }

elseif(($condition == "More Than") && ($noofweeks > $conditionWeeks) && ($supplementType == 'Subtract') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice - $supplementAmnt; }

elseif(($condition == "More Than") && ($noofweeks > $conditionWeeks) && ($supplementType == 'Add') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice + $supplementAmnt; }    

elseif(($condition == "Between") && ($noofweeks >= $startWeek && $noofweeks <= $endWeek) && ($supplementType == 'Add') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice + $supplementAmnt; }

elseif(($condition == "Between") && ($noofweeks >= $startWeek && $noofweeks <= $endWeek) && ($supplementType == 'Substract') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice - $supplementAmnt; }   

//if no conditions match, just return the unaltered basic price back

else { return $basicprice ;}

;} ?>

1 个答案:

答案 0 :(得分:2)

ifs的一个列表可以写成嵌套的if。

最重要的变化:

  1. ($weekno >= $weekStartno && $weekno <= $weekEndno)是其中的一部分 条件,所以它在外面if。
  2. 三种不同的条件, 如果使用或,则与给定条件字符串匹配的值为1 (||)。
  3. 内在的if也是单一的。 Add会产生一个 加成。减法中Subtract
  4. 所有这些都归结为尽可能少地重复这些条件,尽管你应该记住结构应该仍然清晰。在某些情况下,您可能更容易。

    <?php 
    
    // condition = Less Than; More Than; Between
    // noofweeks = this is the number of weeks chosen by the user
    
    function fmsSupplements($condition, $conditionWeeks, $noofweeks, $weekno, $weekStartno, $weekEndno, $basicprice, $supplementAmnt, $supplementType) {
      if ($condition == "Between") {
        // I need to get the start and end values as the data in this parameter should look like 1-17 
        $betweenArray = explode('-',$conditionWeeks);
        $startWeek = $betweenArray[0];
        $endWeek = $betweenArray[1];
      }
    
      if ($weekno >= $weekStartno && $weekno <= $weekEndno)
      {
        if (($condition == 'Less Than' && $noofweeks < $conditionWeeks) ||
            ($condition == 'More Than' && $noofweeks > $conditionWeeks) ||
            ($condition == 'Between' && $noofweeks >= $startWeek && $noofweeks <= $endWeek))
        {
          // You can use a 'switch' as well, instead of if...elseif.
          if ($supplementType == 'Subtract') {
            return $basicprice - $supplementAmnt;
          } elseif ($supplementType == 'Add' {
            return $basicprice + $supplementAmnt;
          }
        }
      }
      return $basicprice;
    } ?>
    

    在不同的设置中,我将中间结果放在单独的变量中,我认为 使它更具可读性。我在这里添加了一些评论,解释了决定:

    function fmsSupplements($condition, $conditionWeeks, $noofweeks, $weekno, $weekStartno, $weekEndno, $basicprice, $supplementAmnt, $supplementType) {
    
      // Create two 'helper' booleans to make the if condition easier.
      $weekInRange = ($weekno >= $weekStartno && $weekno <= $weekEndno);
      $noOfWeeksInRange = 
          ($condition == 'Less Than' && $noofweeks < $conditionWeeks) ||
          ($condition == 'More Than' && $noofweeks > $conditionWeeks);
    
      // Alternatively, you can break the single line above up in multiple 
      // lines, which makes debugging easier:
      //$noOfWeeksInRange = ($condition == 'Less Than' && $noofweeks < $conditionWeeks);
      //$noOfWeeksInRange |= ($condition == 'More Than' && $noofweeks > $conditionWeeks);
    
      if ($condition == "Between") {
        // I need to get the start and end values as the data in this parameter should look like 1-17 
        $betweenArray = explode('-',$conditionWeeks);
        $startWeek = $betweenArray[0];
        $endWeek = $betweenArray[1];
        // Overwrite this variable with the condition that goes with 'Between'.
        // We're already in that if, so we don't need to check 'Condition' again..
        // You could use betweenArray[0] and [1] in the condition below, but using
        // the variables $startWeek and $endWeek does make it more readable.
        $noOfWeeksInRange = ($noofweeks >= $startWeek && $noofweeks <= $endWeek);
      }
    
      // And not this 'if' is even more readable.
      if ($weeksInRange && $noOfWeeksInRange)
      {
          // You can use a 'switch' as well, instead of if...elseif.
          if ($supplementType == 'Subtract') {
            return $basicprice - $supplementAmnt;
          } elseif ($supplementType == 'Add' {
            return $basicprice + $supplementAmnt;
          }
      }
      return $basicprice;
    } ?>