DEV Community

Cover image for Lay Out Your Code Like You'd Lay Out Your House

Lay Out Your Code Like You'd Lay Out Your House

Frederik πŸ‘¨β€πŸ’»βž‘οΈπŸŒ Creemers on July 06, 2018

Imagine you wake up and get up to brush your teeth. You go to the "brooms and brushes" room to get your toothbrush. Then, you go to the "cleaning...
Collapse
 
codemouse92 profile image
Jason C. McDonald

Fantastic! However, this should be paired with a well-tempered inheritance structure, so you're not repeating yourself unnecessarily. You may have seventeen types of lamp in your house, but they all implement the same common wiring and structure.

There are also some things that should be shared among rooms, such as electricity, plumbing, and so forth. No point recreating the entire electrical system for each individual room...that would just make it hard to maintain the system! Implement the common system instead.

Collapse
 
okolbay profile image
andrew

prefer composition over inheritance - extracting non-legit abstractions is coupling your modules without bringing much benefit.

Collapse
 
alainvanhout profile image
Alain Van Hout

Conventions are important

A very important yet undervalued principle πŸ‘.

Collapse
 
s0kil profile image
Daniel Sokil

Are There Any Frameworks That Take Advantage Of This Or A Similar Structure?

Collapse
 
josephmancuso profile image
Joseph Mancuso

The Masonite python framework is very similar to this structure.

Collapse
 
remaster profile image
Daniel Neuman

Angular 1 has "folders-by-feature" in its guidelines. github.com/johnpapa/angular-styleg...

Collapse
 
remaster profile image
Daniel Neuman

Also newer Angular versions have the same recommendations. angular.io/guide/styleguide#folder...

Collapse
 
rhymes profile image
rhymes

I have a basic crud application plus SPA with export functionalities in flask and it's almost stupid how many files you have to modify to add a single field editable by the user: the migration that modifies the db (auto-generated by the changes in the model but still...), the model itself, the server side validation forms, the server side tests, the REST API sending data to the frontend so that the user can store info in the new field, the frontend code receiving the data, the UI, the frontend tests and the export API needs to know about the new field.

The app has so little logic that I feel it's not worth refactoring to abstract a lot of this stuff but everytime the client wants a new field I have to go through the same scattered modifications.

Collapse
 
kmaschta profile image
Kmaschta

Yes, yes, and yes.

Just to pursue the topic, here is an article about that domain-oriented code folders with React: marmelab.com/blog/2015/12/17/react...

It works very well for us.

Collapse
 
ben profile image
Ben Halpern

Awesome post. You definitely had me hooked from the opening metaphor.

Collapse
 
avalander profile image
Avalander

Nice article, thanks for sharing :)

Collapse
 
courier10pt profile image
Bob van Hoove

Here's another advocate :)

K. Scott Allen on feature folders

Collapse
 
okolbay profile image
andrew
Collapse
 
lucianosky profile image
lucianosky

Very good, congratulations! My dad was an architect so your metaphor makes even more sense. :)

Collapse
 
anabella profile image
anabella

Amazing opening paragraph. This kind of question always comes to my head when structuring an app or a project and I never know what makes more sense. Thank you!

Collapse
 
axd7832 profile image
Andrew Diana

It is an absolute blessing when I don't have to ctrl + shift + f to find all implementations of a given function.

Great post Frederik! Keep up the great work.

Collapse
 
jabkarlsson profile image
Ξ±Ξ·βˆ‚Ρ”ΡΡ• кαяℓѕѕση

Reminds me a bit about Conway's law :

en.wikipedia.org/wiki/Conway%27s_law

Collapse
 
auct profile image
auct

I guess you just read chapter about bounded context in ddd book.

Collapse
 
_bigblind profile image
Frederik πŸ‘¨β€πŸ’»βž‘οΈπŸŒ Creemers

I didn't, although DDD is definitely something I'd like to learn more about.