Spotting Code Flaws
00:00 Spot Flaws in Your Code. Refactoring your code contains the risk that it’s a never-ending endeavor. Like a drawing where you keep erasing to perfect your line, you could spend unlimited time looking for improvements in your codebase.
It’s a good idea to limit the scope of your refactoring process. You can do this by glancing over the code and listing the flaws that you want to erase. Don’t be intimidated. In general, the code is viable, and it works. However, as you discovered in the last step, the
forward_to_target_url() functions in your
main.py file aren’t ideal yet.
You may have heard of the Don’t Repeat Yourself, or DRY, principle for keeping your code clean. Lines 42 and 43 look nearly identical. The way that you are constructing the
secret_key variables is a perfect example of non-DRY code. In lines 44 to 49, you are interacting with the database.
This action should not be a direct concern of
create_url, you want to define what data your endpoint expects and what the endpoint should return. Ideally, you should outsource any computation of your data to other functions.
At the end of the function in lines 50 and 51, you are mingling with the database object to create the
schemas.URLInfo response. You’re not trying to save this part to the database, so there’s no harm.
Become a Member to join the conversation.