@@ -132,7 +132,7 @@ from django.http import HttpRequest, HttpResponse
132
132
from words_app.logic import calculate_points
133
133
134
134
def view (request : HttpRequest) -> HttpResponse:
135
- user_word: str = request.GET [' word' ] # just an example
135
+ user_word: str = request.POST [' word' ] # just an example
136
136
points = calculate_points(user_word)
137
137
... # later you show the result to user somehow
138
138
@@ -148,20 +148,20 @@ def _award_points_for_letters(guessed: int) -> int:
148
148
149
149
Awesome! It works, users are happy, your logic is pure and awesome.
150
150
But, later you decide to make the game more fun:
151
- let's make the minimal accoutable letters thresshold
151
+ let's make the minimal accoutable letters threshold
152
152
configurable for an extra challenge.
153
153
154
154
You can just do it directly:
155
155
156
156
``` python
157
- def _award_points_for_letters (guessed : int , thresshold : int ) -> int :
158
- return 0 if guessed < thresshold else guessed
157
+ def _award_points_for_letters (guessed : int , threshold : int ) -> int :
158
+ return 0 if guessed < threshold else guessed
159
159
```
160
160
161
161
The problem is that ` _award_points_for_letters ` is deeply nested.
162
- And then you have to pass ` thresshold ` through the whole callstack,
162
+ And then you have to pass ` threshold ` through the whole callstack,
163
163
including ` calculate_points ` and all other functions that might be on the way.
164
- All of them will have to accept ` thresshold ` as a parameter!
164
+ All of them will have to accept ` threshold ` as a parameter!
165
165
This is not useful at all!
166
166
Large code bases will struggle a lot from this change.
167
167
@@ -177,7 +177,7 @@ from django.http import HttpRequest, HttpResponse
177
177
from words_app.logic import calculate_points
178
178
179
179
def view (request : HttpRequest) -> HttpResponse:
180
- user_word: str = request.GET [' word' ] # just an example
180
+ user_word: str = request.POST [' word' ] # just an example
181
181
points = calculate_points(user_words)(settings) # passing the dependencies
182
182
... # later you show the result to user somehow
183
183
@@ -187,15 +187,15 @@ from typing_extensions import Protocol
187
187
from returns.context import RequiresContext
188
188
189
189
class _Deps (Protocol ): # we rely on abstractions, not direct values or types
190
- WORD_THRESSHOLD : int
190
+ WORD_THRESHOLD : int
191
191
192
192
def calculate_points (word : str ) -> RequiresContext[_Deps, int ]:
193
193
guessed_letters_count = len ([letter for letter in word if letter != ' .' ])
194
194
return _award_points_for_letters(guessed_letters_count)
195
195
196
196
def _award_points_for_letters (guessed : int ) -> RequiresContext[_Deps, int ]:
197
197
return RequiresContext(
198
- lambda deps : 0 if guessed < deps.WORD_THRESSHOLD else guessed,
198
+ lambda deps : 0 if guessed < deps.WORD_THRESHOLD else guessed,
199
199
)
200
200
```
201
201
0 commit comments