首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >写出更美观、更少冗长的代码

写出更美观、更少冗长的代码
EN

Stack Overflow用户
提问于 2012-12-03 08:18:54
回答 1查看 76关注 0票数 1

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

非常感谢

代码语言:javascript
复制
<?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 ;}

;} ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-03 08:44:38

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

最重要的变化:

  1. ($weekno >= $weekStartno && $weekno <= $weekEndno)是所有条件的一部分,所以它在外部if中。
  2. 如果使用or (||),则将与给定条件字符串匹配的三个可变条件放入1。
  3. 内部的if也是单一化的。Add会导致加法运算。subtraction.

中的Subtract

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

代码语言:javascript
复制
<?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;
} ?>

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

代码语言:javascript
复制
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;
} ?>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13675532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档