Code is read 10x more than written (Robert C. Martin). Clean code reduces bugs, speeds onboarding, and makes maintenance predictable.
Meaningful Names
// Bad
$d = 7;
$list = User::where('s', 1)->get();
// Good
$maxLoginAttempts = 7;
$activeUsers = User::where('status', 'active')->get();Small Functions (One Responsibility)
// Good: each function does one thing
function processOrder(OrderRequest $request): Order
{
$validated = $this->validateOrder($request);
$order = $this->createOrder($validated);
$this->chargePayment($order);
$this->updateInventory($order);
$this->notifyCustomer($order);
return $order;
}Use Enums Instead of Magic Values
enum UserRole: int {
case Admin = 1;
case Editor = 2;
case Viewer = 3;
}
enum OrderStatus: string {
case Pending = 'pending';
case Shipped = 'shipped';
case Delivered = 'delivered';
}
if ($user->role === UserRole::Editor) { ... }Early Returns (Guard Clauses)
// Good: flat with guard clauses
function getDiscount(?User $user): float
{
if (!$user) return 0;
if (!$user->isPremium()) return 0.05;
if ($user->ordersCount() <= 10) return 0.1;
return 0.2;
}SOLID Quick Reference
S — Single Responsibility: One reason to change
O — Open/Closed: Open for extension, closed for modification
L — Liskov Substitution: Subtypes must be substitutable
I — Interface Segregation: Many specific > one general
D — Dependency Inversion: Depend on abstractionsClean code reduces cognitive load for the next developer — who might be future you.