Skip to content

Instantly share code, notes, and snippets.

@arnausd23
Last active July 21, 2023 01:16
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save arnausd23/137bab46215d69023729a1b30fb3ec9b to your computer and use it in GitHub Desktop.
Save arnausd23/137bab46215d69023729a1b30fb3ec9b to your computer and use it in GitHub Desktop.
How to organize folders: function-first VS feature-first

How to organize folders: function-first VS feature-first

Function first

Function-first means that we separate each file into the folder they belong to by their usage. For example in a modern frontend project we might have the following folders:

  • /components: Dumb components who only have presentational purposes
  • /containers: Smart components who fetch data and have the logic to parse or modify it before sending it into the dumb components:
  • /store: Contains all the store files like actions, reducers, selectors, etc.

This might be troublesome when having a large application. For example you might see yourself adding a lot of containers for each page and then having problems finding the file you want inside the containers folder. A common approach to solve this is put every container file inside a folder with the feature name, but then we are using feature-first.

Aside from that, one advantatge of using this approach will be if in the future we would like to change some parts of our architecture, like removing Redux or getting rid of the containers/presentational components and have a single components folder we could simply move all the files inside the folder who have all the files, otherwise if we follow the feature-first approach we would have to go folder by folder to move it.


Feature first

Feature-first means that the top-level directories are named after the main features of the app: product, cart, session. This approach scales much better because each new feature comes with a new folder. But you have no separation between the React components and Redux. Changing one of them in the long run is a very tricky job. Additionally you have files that do not belong to any feature. You end up with a folder named common or shared because you want to reuse code across many features in your app.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment