It’s not deeply satisfying is it? There’s definitely some duplication of code here, that
try/
except
occurs twice in
views.py
, and in general things are feeling clunky.
Ran 14 tests in 0.047s
OK
Let’s wait a bit before we do more refactoring though, because we know we’re about to
do some slightly different validation coding for duplicate items. We’ll just add it to our
scratchpad for now:
•
Remove hardcoded URLs from views.py
•
Remove hardcoded URL from forms in
list.html and home.html
•
Remove duplication of validation logic in
views
One of the reasons that the “three strikes and refactor” rule exists is
that, if you wait until you have three use cases, each might be slight‐
ly different, and it gives you a better view for what the common
functionality is. If you refactor too early, you may find that the third
use case doesn’t quite fit with your refactored code…
At least our functional tests are back to passing:
$
python3 manage.py test functional_tests
[...]
OK
We’re back to a working state, so we can take a look at some of the items on our scratch‐
pad. This would be a good time for a commit. And possibly a tea break.
$
git commit -am"enforce model validation in list view"
Refactor: Removing Hardcoded URLs
Do you remember those
name=
parameters in
urls.py
? We just copied them across from
the default example Django gave us, and I’ve been giving them some reasonably de‐
scriptive names. Now we find out what they’re for.
Refactor: Removing Hardcoded URLs
|
187