开发者

Which of these two IF Blocks is better coding practice?

User 2 offers to buy an item from User 1. User 1 can accept or reject. If User 1 accepts, then they will both be able to offer feedback about the transaction.

I have 2 blocks of IF statements. They both work and do the same thing but which is better coding practice?

IF BLOCK 1 checks if which user is there first and then checks if the transaction was accepted or if its still pending

 if ($_SESSION['user_id'] == $seller) {

    if ($row['status'] == 'P') {
        echo '<p>' . get_username_by_id($row['buyer']) . ' has made a bid of ' . $row['price'] . ' for your ' . $row['title'] . '
    <a href="transactions.php?id=' . $transactionid . '&action=accept">Accept</a> / <a href="transactions.php?id=' . $transactionid . '&action=reject">Reject</a><br />';
    } else if ($row['status'] == 'A') {
        echo '<p>' . get_username_by_id($row['buyer']) . ' paid ' . $row['price'] . ' for your ' . $row['title'] . '</p>';
        echo '<a href="feedback.php?id=' . $transactionid . '&action=givefeedback">Give Feedback</a></p>';
    }
} else if ($_SESSION['user_id'] == $buyer) {

    if ($row['status'] == 'P') {
        echo '<p> You have made a bid of ' . $row['price'] . ' for ' . $row['title'] . '</p>';
    } else if ($row['status'] == 'A') {
        echo '<p> You have paid ' . $row['price'] . ' for ' . $row['title'] . '</p>';
        echo '<a href="feedback.php?id=' . $transactionid . '&action=givefeedback">Give Feedback</a></p>';
    }
}

Or

IF BLOCK 2 has only 4 if statements and checks both user and status of transaction at same time

   if ($_SESSION['user_id'] == $seller && $row['status'] == 'P') {
    echo '<p>' . get_username_by_id($row['buyer']) . ' has made a bid of ' . $row['price'] . ' for your ' . $row['title'] . '
    <a href="transactions.php?id=' . $transactionid . '&action=accept">Accept</a> / <a href="transactions.php?id=' . $transactionid . '&action=reject">Reject</a><br />';
} else if ($_SESSION['user_id'] == $buyer && $row['status'] == 'P') {
    echo '<p> You have made a bid of ' . $row['price'] . ' for ' . $row['title'] . '</p>';
} else if ($_SESSION['user_id'] == $seller && $row['status'] == 'A') {
    echo '<p>' . get_username_by_id($row['buyer']) . ' paid ' . $row['price'] . ' for your ' . $row['title'] . '</p>';
    echo '<a href="feedback.php?id=' . $transactionid . '&action=givefeedback">Give Feedback</a></p>';
开发者_如何转开发} else if ($_SESSION['user_id'] == $buyer && $row['status'] == 'A') {
    echo '<p> You have paid ' . $row['price'] . ' for ' . $row['title'] . '</p>';
    echo '<a href="feedback.php?id=' . $transactionid . '&action=givefeedback">Give Feedback</a></p>';
}


The first one shows you the path: if the statuses were to increase in number, you could abstract the functionality into a function with little work. It looks and is cleaner.

I'd also prefer to separate the actual HTML from PHP. Instead of

echo '<p>' . get_username_by_id($row['buyer']) . ' has made a bid of ' 
. $row['price'] . ' for your ' . $row['title'] . '
<a href="transactions.php?id=' . $transactionid . '&action=accept">Accept</a> / 
<a href="transactions.php?id=' . $transactionid . '&action=reject">Reject</a><br />';

I'd prefer

<p>
  <?= get_username_by_id($row['buyer']) ?> has made a bid of 
  <?= $row['price'] ?> for your <?= $row['title'] ?> 
  <a href="transactions.php?id=<?= $transactionid ?>&action=accept">Accept</a> / 
  <a href="transactions.php?id=<?=$transactionid?>&action=reject">Reject</a>
</p>

But to each his own.


This is subject to opinion, but I'm sure that most people will agree that the first one is cleaner because you're removing duplication (the check to see if they are buyer or seller). This will be even more apparent if you had more status types.


I would say block 1 is better coding practice in general, since you do not duplicate information. Having said that, Block 2 more accurately describes the set of Strategy patterned objects which might arise in a different language environment and which could reduce the complexity of your code further.


As a personal choice I prefer the structure of option 1 because of the lower number of conditions. But I would change each else if to elseif to prevent errors because of omitted braces.

To let the code show some common data is used in the output, and the differences in the closing </p> tag of each choice, I would change it into something like this:

$buyerName = get_username_by_id($row['buyer']);
$price = $row['price'];
$title = $row['title'];

if ($_SESSION['user_id'] == $seller) {
    if ($row['status'] == 'P') {
        echo "<p>$buyerName has made a bid of $price for your $title"
           . " <a href='transactions.php?id=$transactionid&amp;action=accept'>Accept</a> /"
           . " <a href='transactions.php?id=$transactionid&amp;action=reject'>Reject</a><br />";
    } elseif ($row['status'] == 'A') {
        echo "<p>$buyerName paid $price for your $title</p>"
           . "<a href='feedback.php?id=$transactionid&amp;action=givefeedback'>Give Feedback</a></p>";
    }
} elseif ($_SESSION['user_id'] == $buyer) {
    if ($row['status'] == 'P') {
        echo "<p> You have made a bid of $price for $title</p>";
    } elseif ($row['status'] == 'A') {
        echo "<p> You have paid $price for $title</p>"
           . " <a href='feedback.php?id=$transactionid&amp;action=givefeedback'>Give Feedback</a></p>";
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜