@@ -49,7 +49,8 @@ Coupling and cohesion are about how tough the components are tied.
49
49
- **High cohesion **. High cohesion is like using the screws. Very easy to disassemble and
50
50
assemble back or assemble a different way. It is an opposite to high coupling.
51
51
52
- When the cohesion is high the coupling is low.
52
+ Cohesion often correlates with coupling. Higher cohesion usually leads to lower coupling, and vice
53
+ versa.
53
54
54
55
Low coupling brings a flexibility. Your code becomes easier to change and test.
55
56
@@ -66,14 +67,14 @@ Before:
66
67
67
68
class ApiClient :
68
69
69
- def __init__ (self ):
70
+ def __init__ (self ) -> None :
70
71
self .api_key = os.getenv(" API_KEY" ) # <-- dependency
71
- self .timeout = os.getenv(" TIMEOUT" ) # <-- dependency
72
+ self .timeout = int ( os.getenv(" TIMEOUT" ) ) # <-- dependency
72
73
73
74
74
75
class Service :
75
76
76
- def __init__ (self ):
77
+ def __init__ (self ) -> None :
77
78
self .api_client = ApiClient() # <-- dependency
78
79
79
80
@@ -94,18 +95,18 @@ After:
94
95
95
96
class ApiClient :
96
97
97
- def __init__ (self , api_key : str , timeout : int ):
98
+ def __init__ (self , api_key : str , timeout : int ) -> None :
98
99
self .api_key = api_key # <-- dependency is injected
99
100
self .timeout = timeout # <-- dependency is injected
100
101
101
102
102
103
class Service :
103
104
104
- def __init__ (self , api_client : ApiClient):
105
+ def __init__ (self , api_client : ApiClient) -> None :
105
106
self .api_client = api_client # <-- dependency is injected
106
107
107
108
108
- def main (service : Service): # <-- dependency is injected
109
+ def main (service : Service) -> None : # <-- dependency is injected
109
110
...
110
111
111
112
@@ -114,7 +115,7 @@ After:
114
115
service = Service(
115
116
api_client = ApiClient(
116
117
api_key = os.getenv(" API_KEY" ),
117
- timeout = os.getenv(" TIMEOUT" ),
118
+ timeout = int ( os.getenv(" TIMEOUT" ) ),
118
119
),
119
120
),
120
121
)
@@ -137,7 +138,7 @@ Now you need to assemble and inject the objects like this:
137
138
service = Service(
138
139
api_client = ApiClient(
139
140
api_key = os.getenv(" API_KEY" ),
140
- timeout = os.getenv(" TIMEOUT" ),
141
+ timeout = int ( os.getenv(" TIMEOUT" ) ),
141
142
),
142
143
),
143
144
)
@@ -182,7 +183,7 @@ the dependency.
182
183
183
184
184
185
@inject
185
- def main (service : Service = Provide[Container.service]):
186
+ def main (service : Service = Provide[Container.service]) -> None :
186
187
...
187
188
188
189
0 commit comments