According to the improving - refactoring existing code to Java code to the PHP code / * * * * * * * * * * I copy of the variable name is * * * * * * * * * * * * /
Copy the code
Refining function 1
$post_name = $_POST['name'];
if(isset($post_name) && !empty($post_name)) {
.
}
$post_age = $_POST['age'];
if(isset($post_name) && !empty($post_name)) {
.
}Copy the code
After the reconstruction
$post_name = $_POST['name'];
if(isSetOrEmpty($post_name)) {
.
}
$post_age = $_POST['age'];
if(isSetOrEmpty($post_age)) {
.
}
function isSetOrEmpty($value) {
if(isset($value) && !empty($value)) {
return true;
}else{
return false;
}
}Copy the code
The original code
function printOwing() {
//print banner
echo '**************************<br/>';
echo 'Banner<br>';
echo '**************************<br/>';
//print details
echo 'name:Lili';
echo 'amount:'.getOutstanding();
}Copy the code
function printOwing() {
printBanner();
printDetails(getOutstanding());
}
function printBanner() {
echo '**************************<br/>';
echo 'Banner<br>';
echo '**************************<br/>';
}
function printDetails($outstanding) {
echo 'name:Lili';
echo 'amount:'.$outstanding;
}Copy the code
Inline the function
If the logic of a function is too simple, cancel the function by moving the code in it to the code calling it
Copy the code
function getRating() {
return moreThanFiveLateDeliveres() ? 2 : 1;
}
function moreThanFiveLateDeliveres() {
return $number > 5;
}Copy the code
After the reconstruction
function getRating() {
return ($number > 5)?2 : 1;
}Copy the code
The original code
$price = $order['price'];
return $price > 1000; Copy the code
return $order['price'] > 1000;Copy the code
Introduce explanatory variables
if($_SESSION['is_login'] > =1 && $_SESSION['is_line'] > =1 && addLoginHistory() && $admin > 0) {
.
}Copy the code
After the reconstruction
$isLogin = $_SESSION['is_login'] > =1;
$isLine = $_SESSION['is_line'] > =1;
$isAdmin = $admin > 0;
if($isLogin && $isLine && addLoginHistory() && $isAdmin) {
.
}Copy the code
If a temporary variable is assigned multiple times (outside the loop), separate temporary variables should be created for each assignment
Copy the code
The original code
$temp = 2*($height + $width);
echo $temp;
$temp = $height + $width;
echo $temp;Copy the code
$perimeter = 2*($height + $width);
echo $perimeter;
$area = $height + $width;
echo $area;Copy the code
Replace nested conditional statements with guard statements
function getPayAmount() {
$resoult = 0;
if($isDead) {
$resoult = deadAmount();
}else{
if($isSeparated) {
$resoult = separatedAmount();
}else{
if($isRetired) {
$resoult = retiredAmount();
}else{
$resoult = normalPayAmount();
}
}
}
return $resoult;
}Copy the code
After the reconstruction
Statements following return will not be executed
Copy the code
function getPayAmount() {
if($isDead) {
return deadAmount();
}
if($isSeparated) {
return separatedAmount();
}
if($isRetired) {
return retiredAmount();
}
return normalPayAmount();
}Copy the code
To be continued……