© 2026 Laravel

KISS Principle - Viết Code Dễ Đọc, Dễ Hiểu (Best Practices cho Developer)

5 phút đọc 49 lượt xem
#fail-fast #error-handling #validation

KISS = Keep It Simple, Stupid

Nhưng trong thực tế production:

KISS không chỉ là “đơn giản” → mà là dễ đọc, dễ hiểu, dễ maintain

Một sự thật quan trọng:

Code được đọc nhiều hơn viết gấp 10–20 lần

Vì vậy:

  • Dev khác sẽ đọc code của bạn
  • Future-you sẽ đọc lại code của chính bạn
  • Reviewer sẽ đọc code mỗi ngày

👉 Nếu code khó đọc → team chậm lại → bug tăng

#2. Anti-pattern: Code “thông minh nhưng khó hiểu”

#❌ PHP thuần - Ví dụ tệ

// Code ngắn nhưng cực khó đọc
function calc($d) {
    return array_reduce(
        array_filter($d, fn($x) => $x['s'] === 'a' && $x['t'] > time() - 86400),
        fn($a, $x) => $a + $x['v'],
        0
    );
}

#❌ Vấn đề

  • Biến không có ý nghĩa ($d, $x, $a)
  • Logic gộp 1 dòng → khó debug
  • Không rõ business đang làm gì

#3. Refactor theo KISS (PHP thuần)

#✅ Code rõ ràng

function calculateActiveOrderValue(array $orders): int
{
    $oneDayAgo = time() - 86400;

    $activeOrders = [];

    // Step 1: Filter
    foreach ($orders as $order) {
        if ($order['status'] === 'active' && $order['timestamp'] > $oneDayAgo) {
            $activeOrders[] = $order;
        }
    }

    // Step 2: Sum
    $total = 0;
    foreach ($activeOrders as $order) {
        $total += $order['value'];
    }

    return $total;
}

#✅ Điểm mạnh

  • Tách step rõ ràng
  • Biến có ý nghĩa
  • Không cần suy luận

#4. Nguyên lý phía sau (Deep Theory)

#4.1 Cognitive Load (tải nhận thức)

Code càng “clever” → càng tốn brain CPU

👉 Mục tiêu:

Người đọc hiểu code trong 5–10 giây

#4.2 Self-Documenting Code

//
function p($d, $o)

// �

function adjustProductPrices(array $products, PriceOptions $options)

👉 Tên function = documentation

#4.3 Explicit > Implicit

// ❌ implicit
return $user && $user->isActive && $user->isAdmin();

// �
 explicit
if (!$user) {
    return false;
}

if (!$user->isActive()) {
    return false;
}

return $user->isAdmin();

#5. Quy tắc thực chiến (Senior/Staff level)

#Rule 1: Một dòng = một ý nghĩa

return $a && $b || $c && !$d;

$isValidA = $a && $b;
$isValidB = $c && !$d;

return $isValidA || $isValidB;

#Rule 2: Đặt tên như nói chuyện

//
function proc($x)

// �

function processExpiredSubscriptions($subscriptions)

#Rule 3: Tránh nested logic sâu

//
if ($user) {
    if ($user->isActive) {
        if ($user->isAdmin) {
            // ...
        }
    }
}

✅ (Guard Clause)

if (!$user) return;
if (!$user->isActive) return;
if (!$user->isAdmin) return;

// logic chính

#Rule 4: Tránh “clever trick”

//
$result = $a ?: $b ?: $c ?: 'default';

// �

if ($a) return $a;
if ($b) return $b;
if ($c) return $c;

return 'default';

#Rule 5: Regex = tài liệu hoặc helper

//
preg_match('/^\S+@\S+\.\S+$/', $email);

// �

function isValidEmail(string $email): bool {
    return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

#6. Mapping sang Laravel (RẤT QUAN TRỌNG)

#6.1 Controller - tránh viết logic phức tạp

public function store(Request $request)
{
    if ($request->email && strlen($request->password) > 8 && ...) {
        // logic dài
    }
}

#6.2 Dùng FormRequest → readable

class StoreUserRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'email' => ['required', 'email'],
            'password' => ['required', 'min:8']
        ];
    }
}

Controller:

public function store(StoreUserRequest $request)
{
    $this->userService->create($request->validated());
}

👉 Controller cực clean

#6.3 Service layer - readable flow

class OrderService
{
    public function createOrder(array $data): Order
    {
        $customer = $this->findCustomer($data['customer_id']);

        $this->ensureCustomerIsActive($customer);

        $total = $this->calculateTotal($data['items']);

        return $this->orderRepository->create([
            'customer_id' => $customer->id,
            'total' => $total
        ]);
    }
}

👉 Đọc như business flow

#6.4 Eloquent - tránh abuse chain

User::where('status', 1)->where('role', 'admin')->where('age', '>', 18)->get();

User::active()
    ->admin()
    ->adult()
    ->get();

Model:

public function scopeActive($q)
{
    return $q->where('status', 1);
}

👉 Query trở nên readable

#7. Khi nào KHÔNG nên KISS quá mức?

#⚠️ Over-simplify

// ❌ quá đơn giản nhưng thiếu context
function process($data)

👉 Không rõ làm gì

#⚠️ Lạm dụng tách nhỏ

function a() {}
function b() {}
function c() {}

👉 Fragment code → khó theo flow

#8. Best Practices (Production)

  • Ưu tiên readability > cleverness

  • Code cho team, không phải cho bản thân

  • Naming quan trọng hơn algorithm (trong business code)

  • Prefer:

    • guard clause
    • early return
    • descriptive method

#9. Interview Questions

KISS khác gì DRY?

Summary:

  • KISS → đơn giản, dễ đọc
  • DRY → không lặp code

Deep:

  • Có thể DRY nhưng vẫn khó đọc
  • Có thể KISS nhưng vẫn duplicate nhẹ (chấp nhận được)
Tại sao code “clever” lại nguy hiểm?

Summary:

  • Khó hiểu → dễ bug

Deep:

  • Reviewer không detect bug
  • Future dev không dám sửa
  • Tăng cognitive load
Khi nào nên viết code ngắn gọn?

Summary:

  • Khi vẫn giữ được readability

Deep:

  • Helper nhỏ OK
  • Nhưng business logic phải rõ ràng

#10. Kết luận

Code tốt không phải code “ngắn nhất”

Mà là code dễ đọc nhất

KISS giúp bạn:

  • Viết code dễ maintain
  • Giảm bug
  • Tăng tốc team