Overview
Hidden Dependency is a situation where, inside a class, methods are silently resolving dependencies, hiding that behavior from the caller. Hidden Dependencies can cause a runtime exception when a caller has not set up the appropriate environment beforehand, which, by itself, is yet another code smell: Required Teardown/Setup Code.
Causation
Objects (which are not stateless) that require a constructor have an empty constructor - the essence of these objects should be passed on to creation, and even better if they are made immutable to avoid Mutable Data Code Smell.
Problems
Each Global Variable is basically a hidden dependency that a tester has to mock.
Mocking is required to test methods that use data outside of its closest scope (class or parameters).
Changing or removing data might unintentionally break code in unexpected places.
It is hard to understand the method's behavior without looking up the functions or variables outside its scope.
Example
1class Customer:
2 pass
3
4customer = Customer()
5
6class Cart:
7 def __init__(self):
8 self.customer = customer # gets customer from global scope
9
10cart = Cart()Refactoring
- Inject Dependencies
Sources
- PARENTAGE
- PARENTAGE