Design Principles are prescriptive guidelines on how to design and structure software. They are actionable rules you follow when writing code.
What Makes Something a Design Principle?
A design principle:
- Prescribes behavior: Tells you HOW to write code
- Is actionable: You can apply it directly in your work
- Guides decisions: Helps you choose between alternatives
- Can be violated: You consciously choose to follow or break it
Examples of prescriptive statements:
- “Favor composition over inheritance”
- “Tell objects what to do, don’t ask for their state”
- “Don’t repeat yourself”
Fundamental Principles
Code Reuse and Abstraction
- DRY (Don’t Repeat Yourself) - About knowledge duplication, not code duplication. Apply the Rule of Three.
- YAGNI (You Aren’t Gonna Need It) - Build only what you need now, not speculative features
- Composition over Inheritance - Favor “has-a” relationships over “is-a” for flexibility
Object-Oriented Design
- Tell, Don’t Ask - Tell objects what to do rather than querying their state
- Law of Demeter - Principle of Least Knowledge, minimize coupling between objects
- Command Query Separation - Methods should either be commands (modify state) or queries (return data), not both
- Read Models and Boundaries - CQS at the architectural level: one write door, inert value objects across boundaries
Class Design (SOLID)
- SOLID Principles - The five fundamental principles of object-oriented class design
How Principles Relate to Concepts
Design principles are grounded in design concepts. Understanding the concepts (like invariants, encapsulation, cohesion) helps you understand WHY the principles matter and when to apply them.
Principles vs. Patterns
- Principles: General guidelines applicable across many situations
- Patterns: Specific, reusable solutions to common problems
See also: Design Patterns