HackSoftware / Django-Styleguide

Django styleguide used in HackSoft projects
MIT License
5k stars 511 forks source link

Question: File structure / organization? #107

Closed miketheman closed 1 year ago

miketheman commented 2 years ago

Hello! Really enjoyed reading the guide, thanks a ton for putting it together!

I was also looking for some language on patterns of organizing files, directories, and how to scale that kind of structure as well. I didn't find anything specific - did I miss it, or is that something covered in other guides?

Questions like: when to split files up, how to organize them into namespaces/packages, how to organize test cases to make it easy to find and relate to the source, when is something an "app" and wen it isn't, those kinds of repo-structure items. Is that something you'd consider expanding the guide to include?

RadoRado commented 1 year ago

@miketheman :wave:

Sorry for the late reply! We somehow missed the notification for a new issue.

To answer your question: File structure & organization really depends on your taste & the specific context of your application.

I can share few rules of thumb that we follow:

  1. You want things, that belong together, to be as closely located as possible. If they can be in a single file - that's great! That's usually referred to as "cohesion"
  2. In order to isolate something in a namespace, you can simply create Python modules. Whether you put the code in some_module/__init__.py or some_module/some_submodule.py and export the things from __init__.py - this depends on personal taste.
  3. Additionally, classes are also a great tool to "namespace" something. A good example for this are the "class-based" service - https://github.com/HackSoftware/Django-Styleguide#example---class-based-service - I want all those methods to be under a single namespace & in this scenario, putting them in a class gives me that. Otherwise, I need to specifically extract them to a separate module (which is also fine, but for something that's below 100 lines of code - it looks like an overkill)
  4. When to split? Whenever you feel that the current file is holding too many things and there are clear boundaries between those things. You can then split by those actual boundaries. Usually, a "split" means - "I need more namespaces to organize the things that I have"
  5. When speaking about Django, an abstraction that we've found to work quite well is having "sub-apps". Apps within apps. You can do this as simply as just import everything in the top-level app, to make things discoverable , or you can try be more sophisticated with existing abstractions or build your own.

I think those points summarize how we look at things, when it comes to namespacing & splitting.

For me, the guiding principle is - "things that belong together should be closely located, potentially in the same file".

Please, let me know if this helps you in some shape or form & if you have additional questions - feel free to ask them :raised_hands: