Skip to content

Instantly share code, notes, and snippets.

@dungle-scrubs
Created January 15, 2026 09:22
Show Gist options
  • Select an option

  • Save dungle-scrubs/77c660ba2fcde28d389db60d6d8ef732 to your computer and use it in GitHub Desktop.

Select an option

Save dungle-scrubs/77c660ba2fcde28d389db60d6d8ef732 to your computer and use it in GitHub Desktop.
AI coding standards - OOP, testing
## OOP Best Practices
### Composition & DI
- Favor composition over inheritance (prefer constructor injection over deep inheritance trees)
- Inject dependencies via constructors or factories
- Avoid service locators or hidden globals
### SOLID Principles
- Always follow SOLID principles
### Domain Modeling
- Use value objects for invariants (e.g., `Email`, `Money`)
- Encapsulate invariants and validation in the model
- Prefer rich domain methods over anemic data structures
- Combat primitive obsession: wrap primitives with validation
### Error Handling
- Create typed error hierarchies with discriminants
- Never throw strings; use `Error` subclasses
- Bubble domain errors, map at boundaries (HTTP/UI)
### Patterns
- Tell, Don't Ask: call behavior methods directly, don't inspect then mutate
- Law of Demeter: interact only with immediate collaborators, avoid long call chains
- Make impossible states impossible: encode valid states in types (discriminated unions)
- Replace conditionals with polymorphism: use Strategy pattern over if/else chains
- Fail fast: use guard clauses for preconditions
- Observers & Events: decouple publishers from subscribers
### Testing
- Design for testability: use constructor DI so test doubles can be injected without mocking frameworks
- Test public behavior, not private implementation
- Mock at architectural boundaries; avoid over-mocking internals
### Heuristics
- Use inheritance only for true is-a relationships
- Permit minimal getters when necessary; encapsulate decisions in methods
- If strict adherence impairs clarity, annotate exceptions with rationale
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment