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のその1つのリストは、ネストされたifとして記述できます。

最も重要な変更:

  1. ($weekno >= $weekStartno && $weekno <= $weekEndno)はすべての条件の一部であるため、外側の場合にあります。
  2. または()を使用すると、指定された条件文字列に一致する3つのさまざまな条件が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 に答える