utils.py πŸ™ˆ

I have written the following statements on slack today in a more project-related manner and thought this could make a small dev-tip post as well. Here goes πŸ’ͺ You basically don’t ever want to create a file called utils.py, or helpers.py (you also don’t want core.py or common.py but let’s focus on utils for now). There are a few reasons to that: πŸ›  putting things into utils.py makes it a bag full of everything from simple datetime calculations to sophisticated validators and Class mixins πŸ›  utils is a nondescriptive name so nobody including you will know what has been put there in two weeks and more importantly where to look if someone has already written that util you are looking for (because they might put it in it’s proper place) πŸ›  there is always a better place with a better name than utils (examples below)

How to approach the topic in an already operating project:

πŸ›  it’s good to move functions out from utils.py to their corresponding utils/proper_module.py each time you find an example and talk with your team about the whole purpose of moving that out

πŸ›  if something is more generic than a simple module in your Django app / python module, you can move it higher in directory structure to the more generic utils/proper_module.py file. So generally speaking if you want utils in your project, just make it more structured. I prefer the name helpers more than utils, but no strings attached here - it’s more important to keep consistency within the project, not looking at your own personal preferences.

Some examples of moves from utils.py:

🌟 amount_of_cat_food_validator β†’ helpers/validators

🌟 CatPurrAwareMixin β†’ helpers/mixins

🌟 calculate_days_until_christmas β†’ helpers/datetime

🌟 a few topic related files can reside in their own directory, so form related modules (like new field types) will reside in helpers/forms/

πŸ“– http://deviq.com/naming-things/

πŸ“– https://github.com/erikras/react-redux-universal-hot-example/issues/808

πŸ“– https://softwareengineering.stackexchange.com/a/175190