CHAPTER 10
Intermediate
Object-Oriented Clean Code
Updated: May 16, 2026
30 min read
# CHAPTER 10
Object-Oriented Clean Code
1. Introduction
The transition from procedural programming (a long list of instructions) to Object-Oriented Programming (OOP) was meant to make code easier to manage. However, if used poorly, OOP creates a tangled nightmare of deep inheritance trees and globally accessible, mutable state. Clean Code applies strict rules to how we design our objects. An object is not just a glorified array of data; it is a behavioral entity that fiercely protects its internal state. In this chapter, we will explore Object-Oriented Clean Code. We will master Encapsulation to hide data, Abstraction to hide implementation details, and explore the golden rule of modern software architecture: "Favor Composition over Inheritance."2. Learning Objectives
By the end of this chapter, you will be able to:- Distinguish between true Objects (Behavior) and Data Structures (State).
- Apply strict Encapsulation (hiding internals).
- Understand why deep Inheritance hierarchies are fragile and dangerous.
- Master the "Composition over Inheritance" design pattern.
- Design highly cohesive, loosely coupled classes.
3. Objects vs. Data Structures
These two concepts are opposites, and confusing them creates bad code.- Data Structures (DTOs): Expose their data (public variables) and have *no meaningful behavior* (no complex functions). They are just dumb containers for moving data around.
- Objects: Hide their data (private variables) and expose *behavior* (public functions). You do not ask an object for its data; you tell an object to do something.
- *The Rule:* Never create a hybrid. An entity should either be a pure Data Structure or a pure Object.
4. Encapsulation (Information Hiding)
Encapsulation is the act of hiding the internal state of an object from the outside world.-
The Problem: If you make an
$accountBalancevariablepublic, any random file in your application can do$user->accountBalance = 9000000;. Your system's data integrity is destroyed.
-
The Solution: Make
$accountBalanceprivate. The only way to change it is by calling a public method:$user->deposit(100). Inside thedeposit()method, the object enforces business rules (e.g., checking if the deposit is positive). The object acts as an impenetrable fortress protecting its data.
5. The Law of Demeter
The Law of Demeter says a module should not know about the innards of the objects it manipulates.-
The Train Wreck (Violation):
$customer->getWallet()->getCreditCard()->charge(50);
-
The Clean Way:
$customer->charge(50);
6. Composition over Inheritance
For decades, developers were taught to use Inheritance (extends) to share code. This is now considered an anti-pattern.-
The Problem (Fragile Base Class): You have a
Vehicleclass.CarandBoatinherit from it. You add adriveOnRoad()method toVehicle. Suddenly, yourBoatclass breaks or inherits nonsensical methods. Deep inheritance trees are rigid and fragile.
- The Solution (Composition): Instead of *being* a Vehicle, a Car *has* an Engine, *has* Wheels, and *has* a Steering system. You build objects by composing smaller, single-responsibility objects together. This allows infinite flexibility without breaking inheritance trees.
7. Diagrams/Visual Suggestions
*Inheritance vs Composition*
txt
8. Best Practices
- Small Classes: Just like functions, classes should be small. A class with 2,000 lines is a "God Class" that knows too much and does too much. It violates the Single Responsibility Principle. A class should be easily understandable in a few minutes.
9. Common Mistakes
-
Getters and Setters Everywhere: A developer makes all variables
private, but then immediately generates aget()andset()method for every single variable. This is not Encapsulation! If you have a public setter for every variable, the data is essentially public. Only expose methods that represent valid business behaviors.
10. Mini Project: Refactor to Composition
Scenario: A game developer used inheritance poorly. *Before (Inheritance Problem):*class Character {}
class FlyingCharacter extends Character {}
class MagicCharacter extends Character {}
*What if a character flies AND uses magic? Multiple inheritance isn't allowed in PHP/Java.*
*After (Composition):*
php
11. Practice Exercises
- 1. Explain the "Law of Demeter" (The Principle of Least Knowledge). Why are "Train Wreck" method chains dangerous for code maintainability?
- 2. Differentiate between a pure Object and a pure Data Structure.
12. MCQs with Answers
Question 1
What is the primary purpose of Encapsulation in Object-Oriented design?
Question 2
Modern software architecture strongly recommends "Favoring Composition over Inheritance." Why?
13. Interview Questions
- Q: A junior developer makes all class properties private, but generates a public Getter and Setter for every single property, claiming they have achieved "Encapsulation." Do you agree or disagree? Explain your reasoning.
- Q: Explain the "Fragile Base Class" problem. Give a real-world example of how a change to a parent class can unintentionally destroy a child class.
- Q: How does Dependency Injection rely heavily on the concept of Composition?