How to organize source code files? By function or type?

In my early days of coding, I would like to group classes that functioned in a similar way. For instance:

  • Data Transfer Objects
    • Object A
    • Object B
  • Dialogues
    • Dialog A
    • Dialog B

After a while, I started to upset me that when I was working on a certain part of the application, I would have to spin around to put it together.

In the past few years, I have been trying to organize things by signs. Classes that are commonly used together, such as database objects, I still hold together. I think this even makes sense for things like websites:

  • Page A
    • Images
    • Resource 1
    • Dialog 1
  • Page B
    • Images
    • Resource 2
    • Dialog 2

Is this the best way to do this? Does anyone have a good rule of thumb?

+4
source share
3 answers

For Java, packages are a reusable unit.

For Python, modules (as well as packages) are reusable units.

The package must be standalone.

If you put all the data objects in one big package, you don’t have something terribly reusable. You may not need all of these definitions of data transfer objects.

If you combine things "entity" - a model, views, controls, data access, etc. - then you have a reusable module that can be used in other applications.

+8
source

The Package-by-Feature approach seems reasonable and works for me, of course, for Java ... how often do you want to pack your data access level, and is this a great new feature?

I thought the analysis “Package by function, not layer” on javapractises.com was quite easy to read and covered a couple that I did not think, for example, how a package by function works in areas other than programming.

+4
source

You have to do it the way other code using your language / tools does. This is not an agnostic language question; some languages ​​/ tools expect a relationship between “file organization” and “what's in these files,” and you should respect that.

0
source

All Articles