1

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

非常感谢

<?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 ;}

;} ?>
4

1 回答 1

2

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

最重要的变化:

  1. ($weekno >= $weekStartno && $weekno <= $weekEndno)是所有条件的一部分,所以它在外部 if 中。
  2. ||如果使用或 ( ) ,则与给定条件字符串匹配的三个不同条件放入 1 。
  3. 内部 if 也是单数。Add结果是加法。Subtract在减法中。

一切都归结为尽可能少地重复条件,尽管您应该记住结构仍然应该是清晰的。在某些情况下,您的方式可能更容易。

<?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;
} ?>
于 2012-12-03T00:44:37.840 回答