Hidden Dependencies — Code Smells Catalog Skip to content

Hidden Dependencies

Data Dealers Data Code SmellDesign Smell Between Class

The class works perfectly in the developer's environment and crashes in production. You read the stack trace, search every constructor, and find nothing. The dependency was silently resolved from...

2 min read 2 sources

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

🔗
Coupling

Each Global Variable is basically a hidden dependency that a tester has to mock.

🧪
Hard to Test

Mocking is required to test methods that use data outside of its closest scope (class or parameters).

Error-Prone

Changing or removing data might unintentionally break code in unexpected places.

📖
Decreased Comprehensibility

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()
PYTHON

Refactoring

  • Inject Dependencies

Sources

Browse All 56 Smells