Skip to content

Instantly share code, notes, and snippets.

@craig-davis
Last active April 15, 2017 17:24
Show Gist options
  • Save craig-davis/19004e7f3b96e500a0cdc9a338cb7784 to your computer and use it in GitHub Desktop.
Save craig-davis/19004e7f3b96e500a0cdc9a338cb7784 to your computer and use it in GitHub Desktop.
Reducing Complexity by Refactoring with Guard Clauses in PHP and JavaScript
<?php
function calculateSalePrice(Item $item, $discount = 0) : int
{
$price = 0;
if ($discount == 0) {
$price = $item->getPrice();
}
else {
$price = calculateDiscountedPrice($item->getPrice(), $discount);
}
return $price;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment