Programming: Design
From wikinotes
SOLID Design Principles
https://stackify.com/interface-segregation-principle/
enum vs object
Pros/Cons
- enums are better than magic-numbers (less misleading)
- enums may lead to duplicated conditionals (leading to shotgun-surgery)
- injected-objects can fully encapsulate a behaviour (no conditionals)
- injected objects are more intimidating to users (requires reading, finding, satisfying injected obj deps)
Strategy
use enums
- to represent related objects that do not imply specific behaviour (ex: weekdays)
- for data that gets serialized (where behaviours cannot be encoded)
use injected-objects:
- everywhere else
- better for testing
- better for flexibility
- better for introducing new behaviours without introducing breaks
Exceptions
- beginner-friendly directing users to use other objects can be tricky. In this instance, I'll create the self-encapsulating object (still simplifies testing), but accept a string value as well. It is not perfect, but it should keep everyone happy.
See Also