Chapter

Core principles

The everyday design principles that guide good code: KISS, DRY, YAGNI, composition, the Law of Demeter, and separation of concerns.

Design Patterns & Principles 6 Lessons from courses

About this chapter

This chapter covers the principles you reach for every day - keep code simple, avoid duplicated knowledge, don't build for imagined futures, prefer composition, keep objects from reaching too far, and give each class one concern.

Lessons from courses

  1. 1 KISS - Keep It Simple The KISS principle in plain terms: prefer the simplest code that works. Why clever one-liners are a liability, shown with a simple vs over-clever PHP example.
  2. 2 DRY - Don't Repeat Yourself The DRY principle done right: one source of truth per piece of knowledge - plus the trap where a wrong abstraction costs more than a little duplication.
  3. 3 YAGNI - You Aren't Gonna Need It The YAGNI principle explained: building for speculative future needs is a cost, not an investment. Where over-engineering hides, with a clear PHP example.
  4. 4 Composition over inheritance Why composition over inheritance holds up as systems grow: has-a beats deep is-a, the fragile base class problem, and a PHP example swapping one for the other.
  5. 5 The Law of Demeter The Law of Demeter, made practical: don't talk to strangers, avoid long a->b->c->d chains, and apply Tell, Don't Ask. With a clear PHP example.
  6. 6 Separation of concerns Separation of concerns, made concrete: each class handles one job. Why mixing business logic, persistence and formatting makes code hard to change, in PHP.
Design Patterns & Principles Go to course