General Software Design Principles
6. DRY (Don’t Repeat Yourself)
Definition. Avoid duplicating knowledge or logic; keep a single authoritative representation of each concept.
Rationale. Duplicated knowledge means a change must be made in many places, and they drift. One source of truth keeps behavior consistent and reduces the surface for bugs.
Example. Store a discount rule in one function rather than copying the formula into every checkout path. When the rule changes, one edit suffices.
Anti-pattern / failure mode. Copy-paste programming: the same calculation appears in controllers, services, and reports, each slightly different. Review signal: near-identical blocks with small variations.
Related. Single Source of Truth (#90), Common Closure (#32).
7. KISS (Keep It Simple, Stupid)
Definition. Prefer simple solutions over unnecessary complexity; choose the smallest design that solves the problem correctly.
Rationale. Complex designs are harder to understand, test, and change. Simplicity reduces defect rates and onboarding cost; complexity should be justified by a real need.
Example. Use a plain function instead of a configurable rule engine when only two cases exist today.
Anti-pattern / failure mode. Premature abstraction: frameworks, plugins, and indirection layers built for hypothetical future needs. Review signal: code that is harder to read than the problem it solves.
Related. YAGNI (#8), Economy of Mechanism (#67).
8. YAGNI (You Aren’t Gonna Need It)
Definition. Don’t implement functionality until it is actually needed; defer speculative features.
Rationale. Unused features still cost design, implementation, testing, and maintenance, and they constrain future change. Building for real requirements keeps the system lean and adaptable.
Example. Don’t add multi-currency support until a customer actually needs a second currency.
Anti-pattern / failure mode. “Just in case” features and hooks that are never exercised; dead code paths that rot. Review signal: configuration flags or extension points with no callers.
Related. KISS (#7), Small Batch (#87).
9. Separation of Concerns (SoC)
Definition. Separate systems into distinct areas of responsibility; keep unrelated concerns from mixing in the same module.
Rationale. Mixed concerns make changes risky because a fix in one area can break another. Clear boundaries let each part evolve and be tested independently.
Example. Keep HTTP parsing, business rules, and persistence in separate layers rather than one handler doing all three.
Anti-pattern / failure mode. A controller that validates input, computes pricing, writes to the database, and sends email. Review signal: a single function with multiple reasons to change.
Related. Single Responsibility (#1), High Cohesion, Low Coupling (#28).
10. Principle of Least Astonishment (POLA)
General-behavior scope — for API-surface predictability see Principle of Least Surprise (#79).
Definition. Software behavior should match user and developer expectations; avoid surprising side effects and inconsistent conventions.
Rationale. Surprises cause misuse and bugs. Predictable behavior reduces cognitive load and makes APIs and systems trustworthy.
Example. A save() method that also sends a notification astonishes callers; name it saveAndNotify() or split the actions.
Anti-pattern / failure mode. Hidden side effects in getters or query methods; inconsistent naming. Review signal: methods whose names do not describe their effects.
Related. Principle of Least Surprise (#79), API Consistency (#82).
11. Law of Demeter (LoD)
Definition. Objects should communicate primarily with their immediate collaborators; avoid reaching through chains of objects to distant parts of the system.
Rationale. Long chains couple callers to intermediate structures and break when those structures change. Talking to direct collaborators keeps coupling shallow and encapsulation intact.
Example. Instead of order.getCustomer().getAddress().getCity(), ask order.getShippingCity().
Anti-pattern / failure mode. “Train wreck” expressions chaining getters across multiple objects. Review signal: code that knows the shape of several objects at once.
Related. Encapsulation (#13), Information Hiding (#14).
12. Composition over Inheritance
Definition. Prefer assembling behavior through composition rather than deep inheritance hierarchies; compose small, focused objects instead of subclassing.
Rationale. Inheritance binds behavior at compile time and creates fragile base classes; composition lets you swap parts at runtime and keeps classes small. Deep hierarchies amplify change impact.
Example. Give a Character a Weapon object it can swap, rather than subclassing SwordCharacter, BowCharacter, and so on.
Anti-pattern / failure mode. Deep inheritance trees where a base-class change ripples through dozens of subclasses. Review signal: subclasses that override most inherited methods.
Related. Open/Closed (#2), Dependency Inversion (#5).
13. Encapsulation Principle
Definition. Hide internal implementation details and expose controlled interfaces; protect invariants by limiting direct access to state.
Rationale. Uncontrolled access lets callers put objects into invalid states and couples them to representation. Controlled interfaces preserve invariants and allow the implementation to change freely.
Example. Make fields private and expose deposit()/withdraw() that enforce a non-negative balance, instead of public mutable fields.
Anti-pattern / failure mode. Public mutable state with external code enforcing rules; “anemic” objects with no behavior. Review signal: setters that allow invalid states.
Related. Information Hiding (#14), Program to an Interface (#15).
14. Information Hiding Principle
Definition. Hide design decisions likely to change behind stable boundaries; keep volatile details out of public contracts.
Rationale. By hiding what changes, you limit the blast radius of a change to the module that owns it. Stable interfaces let the rest of the system depend on contracts, not details.
Example. Hide the storage engine behind a repository interface so swapping SQL for a document store touches one adapter.
Anti-pattern / failure mode. Leaking database schemas, file formats, or third-party types through public APIs. Review signal: changing a library version forces broad code changes.
Related. Encapsulation (#13), Dependency Rule (#22).
15. Program to an Interface, Not an Implementation
Definition. Depend on contracts rather than concrete implementations; code against abstractions so implementations can vary freely.
Rationale. Concrete dependencies lock you to one implementation and make substitution, testing, and evolution hard. Abstractions decouple consumers from producers.
Example. A Notifier interface with EmailNotifier and SmsNotifier implementations lets callers switch channels without code changes.
Anti-pattern / failure mode. Direct instantiation of concrete classes in business logic; static calls to specific implementations. Review signal: code that cannot be tested with a fake.
Related. Dependency Inversion (#5), Hexagonal Architecture (#23).
16. Fail Fast Principle
Definition. Detect and report errors as early as possible; surface problems at the boundary where they occur instead of letting them propagate.
Rationale. Late failures are harder to diagnose and may corrupt state downstream. Early detection keeps errors local, messages clear, and recovery simple.
Example. Validate required fields at the API boundary and return a 400 immediately, rather than failing deep in a batch job hours later.
Anti-pattern / failure mode. Swallowing errors, returning nulls, or deferring validation; “fail slow” systems that produce wrong results far from the cause. Review signal: errors surfaced in unrelated components.
Related. Defensive Programming (#17), Fail Securely (#66).
17. Defensive Programming
Definition. Anticipate invalid inputs and unexpected states; validate assumptions and handle edge cases deliberately.
Rationale. The outside world is unreliable: bad data, race conditions, and partial failures happen. Defensive code keeps the system robust instead of crashing on the first surprise.
Example. Check for null/empty inputs, clamp ranges, and use assertions for invariants that must hold.
Anti-pattern / failure mode. Assuming inputs are always valid; trusting internal callers blindly. Review signal: production crashes on malformed but plausible input.
Related. Fail Fast (#16), Complete Mediation (#65).
18. Principle of Least Privilege (PoLP)
Identity/access scope (who may do what) — for minimizing shared infrastructure see Least Common Mechanism (#68), and for splitting critical actions see Separation of Privilege (#69).
Definition. Grant only the permissions necessary to perform a task; limit access so a compromised component cannot do more than it needs.
Rationale. Excess permissions amplify the impact of a bug or breach. Minimal permissions contain damage and reduce the attack surface.
Example. A report generator gets read-only database access, not admin credentials.
Anti-pattern / failure mode. Shared admin accounts, wildcard permissions, services running as root. Review signal: a compromised low-risk component can alter critical data.
Related. Zero Trust (#62), Defense in Depth (#63), Separation of Privilege (#69).
19. Explicit over Implicit
Definition. Make important behavior and dependencies clear; favor visible wiring over hidden convention or magic.
Rationale. Implicit behavior is easy to miss and hard to debug. Explicit wiring makes dependencies and effects obvious, aiding review and maintenance.
Example. Inject dependencies through constructors instead of resolving them from a global registry.
Anti-pattern / failure mode. Magic strings, auto-wiring by naming convention, hidden global state. Review signal: “where does this come from?” during code review.
Related. Convention over Configuration (#20), Explicit State Ownership (#110).
20. Convention over Configuration
Definition. Use sensible defaults to reduce unnecessary configuration; let common cases work with little or no setup.
Rationale. Most projects follow the same patterns; forcing configuration for the common case wastes time and invites errors. Defaults capture the common case while still allowing overrides.
Example. A framework that finds controllers in a standard directory without explicit registration.
Anti-pattern / failure mode. Over-configured systems where every default must be specified; or magic defaults that are hard to discover. Review signal: long configuration files for standard setups.
Related. Explicit over Implicit (#19), API Consistency (#82).