api: YAML JSON TOON Database version: 1.0.0 format: json dataset: slug: solid-principles title: SOLID Principles description: Five design principles for writing maintainable object-oriented code. summary: "The five SOLID principles of object-oriented design with a description and an example each, followed by related design principles: DRY, KISS, YAGNI, the Law of Demeter, composition over inheritance, separation of concerns and Tell, Don't Ask." use_when[2]: You need the principle behind a design decision or a code-review comment.,You are teaching object-oriented design. caveats[2]: Principles are guidelines that conflict at times; DRY can push toward abstractions that YAGNI warns against.,"Taken to extremes, they produce over-engineered code." related[4]: creational-patterns,structural-patterns,behavioral-design-patterns,design-pattern-anti-patterns category: Design Patterns category_slug: design-patterns tags: "design-patterns,solid,oop,software-engineering" view_count: 0 entry_count: 12 fields[3]{section,field,type,example}: data,principle,string,S - Single Responsibility data,description,string,A class should have only one reason to change. data,example,string,Separate UserAuth from UserProfile. data[12]{principle,description,example}: S - Single Responsibility,A class should have only one reason to change.,Separate UserAuth from UserProfile. O - Open/Closed,"Open for extension, closed for modification.",Add new payment types without changing existing processor. L - Liskov Substitution,Subtypes must be substitutable for their base types.,A Square should not extend Rectangle if width=height invariant breaks. I - Interface Segregation,Prefer many small client-specific interfaces over one large interface.,Separate Printer and Scanner interfaces. D - Dependency Inversion,"Depend on abstractions, not concrete implementations.","Controller depends on ILogger interface, not FileLogger." DRY - Don't Repeat Yourself,"Every piece of knowledge should have a single, authoritative representation.",Define a validation rule once and reuse it instead of copying it into several handlers. KISS - Keep It Simple,Prefer the simplest design that solves the problem.,Use a plain function before introducing a class hierarchy. YAGNI - You Aren't Gonna Need It,Do not add functionality until it is actually needed.,Skip a plugin system until a second implementation exists. Law of Demeter,"A unit should talk only to its immediate collaborators, not reach through them.",Call order.total() instead of order.getCustomer().getAccount().getBalance(). Composition over Inheritance,Combine small objects to get behavior instead of extending class hierarchies.,Give a Car an Engine object rather than making Car a subclass of Engine. Separation of Concerns,Divide a program into parts that each address a distinct concern.,"Keep data access, business rules and presentation in separate layers." "Tell, Don't Ask",Tell objects what to do instead of asking for their state and acting on it.,Call account.withdraw(amount) rather than reading the balance and deciding outside.