Core Design Principles Flashcards
(10 cards)
The Single Responsibility Principle (SRP)
- Design classes in such a way that there is ideally only a single reason to change a class
- Leads to smaller and more cohesive classes
- Leads to less complex classes
Functional cohesion
- Group entities* that change for the same reasons
- Separate entities that change for different reasons
encapsulation
The act of keeping both the data and the computation
together to limit the number of contact points between different parts of your system
often violated through references that escape
advanatages of encapsulation
- Understanding a piece of code in isolation is easier
- Using a piece of code becomes less error-prone
- Changing a piece of code less likely breaks something else
3 ways in which references can escape (slides)
- Returning a reference to an external object
- Storing an external reference internally
- Leaking a reference through a shared structure
+/- of immutability
+ You can share information without breaking encapsulation
+ You avoid temporal method dependencies (invocation order)
+It leads to thread safety
+ It allows the caching of objects
- You tend to create more objects
- Decreased performance efficiency (more garbage collection)
How to make objects immutable
- Ensure that all the fields of your class are:
- Either private and not changed by any instance method
- Or immutable by default
- Primitive types or enumerations
- final
- Expose internal data consciously
- Extended interface
- Return copies
- Via dedicated design patterns
Inherent complexity
Unavoidable domain complexity
Accidental complexity
Avoidable technical complexity introduced through suboptimal design
types of complexity
Structural complexity (coupling): The number and strength of relationship between the structures in your program (packages, classes, methods)
reading complexity
data complexity
decision complexity