Rumah >pembangunan bahagian belakang >tutorial php >Menguasai Kod Bersih dalam PHP: Pengajaran Utama daripada Perjalanan Pengekodan Saya
"Menguasai Kod Bersih dalam PHP: Pengajaran Utama daripada Perjalanan Pengekodan Saya" memfokuskan pada peraturan dan contoh praktikal dan praktikal untuk membantu pembangun PHP menulis kod yang bersih, boleh diselenggara dan cekap. Di sini, kami akan memecahkan peraturan penting ini kepada bahagian yang boleh dihadam, setiap satunya disertakan dengan contoh.
Contoh:
// BAD: $x = 25; function d($a, $b) { return $a + $b; } // GOOD: $age = 25; function calculateSum($firstNumber, $secondNumber) { return $firstNumber + $secondNumber; }
Penerangan:
Contoh:
// BAD: class Order { public function calculateTotal() { // logic to calculate total } public function sendInvoiceEmail() { // logic to send email } } // GOOD: class Order { public function calculateTotal() { // logic to calculate total } } class InvoiceMailer { public function sendInvoiceEmail() { // logic to send email } }
Penerangan:
Contoh:
// BAD: function processOrder($order) { // Validate order if (!$this->validateOrder($order)) { return false; } // Calculate total $total = $this->calculateTotal($order); // Send invoice $this->sendInvoiceEmail($order); return true; } // GOOD: function processOrder($order) { if (!$this->isOrderValid($order)) { return false; } $this->finalizeOrder($order); return true; } private function isOrderValid($order) { return $this->validateOrder($order); } private function finalizeOrder($order) { $total = $this->calculateTotal($order); $this->sendInvoiceEmail($order); }
Penerangan:
Contoh:
// BAD: if ($user->age > 18) { echo "Eligible"; } // GOOD: define('MINIMUM_AGE', 18); if ($user->age > MINIMUM_AGE) { echo "Eligible"; }
Penerangan:
Contoh:
// BAD: $total = $itemPrice * $quantity; $finalPrice = $total - ($total * $discountRate); $cartTotal = $cartItemPrice * $cartQuantity; $finalCartPrice = $cartTotal - ($cartTotal * $cartDiscountRate); // GOOD: function calculateFinalPrice($price, $quantity, $discountRate) { $total = $price * $quantity; return $total - ($total * $discountRate); } $finalPrice = calculateFinalPrice($itemPrice, $quantity, $discountRate); $finalCartPrice = calculateFinalPrice($cartItemPrice, $cartQuantity, $cartDiscountRate);
Penerangan:
Contoh:
// BAD: function processPayment($amount) { if ($amount > 0) { if ($this->isPaymentMethodAvailable()) { if ($this->isUserLoggedIn()) { // Process payment } } } } // GOOD: function processPayment($amount) { if ($amount <= 0) { return; } if (!$this->isPaymentMethodAvailable()) { return; } if (!$this->isUserLoggedIn()) { return; } // Process payment }
Penerangan:
Contoh:
// BAD: // Increment age by 1 $age++; // GOOD: // User's birthday has passed, increase age by 1 $age++;
Penerangan:
Example:
// Before Refactoring: function getDiscountedPrice($price, $isHoliday) { if ($isHoliday) { return $price * 0.9; } else { return $price; } } // After Refactoring: function getDiscountedPrice($price, $isHoliday) { return $isHoliday ? $price * 0.9 : $price; }
Description:
These essential rules—derived from the journey of coding cleanly in PHP—make your code more readable, maintainable, and scalable. By applying these principles consistently, you ensure that your code is easy to understand and modify over time, regardless of the complexity of your projects.
Atas ialah kandungan terperinci Menguasai Kod Bersih dalam PHP: Pengajaran Utama daripada Perjalanan Pengekodan Saya. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!