DEV Community

Cover image for Folder Structure for Modern Web Applications

Folder Structure for Modern Web Applications

Obaseki Noruwa on April 01, 2023

It is critical to create a maintainable folder structure while developing web apps, having the right files in the correct folder helps organize you...
Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

I’ve tried completely flat projects before that’s had some mileage before it got too much, then I added some folders but this subject, it’s not actually important because we have amazing find in files features built into everything, don’t get hung up on it ✌️

Collapse
 
gunslingor profile image
gunslingor

I'm actually in complete disagreement with this statement... its only true if you've worked there for years and know the application history. If not, finding via string search in 100k lines of code is often, more often than not, nightmarish.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

I use for example ECSS a system that is designed to group css by a 3 letter namespace and that is a point that conventions are often found more in files than folders so identifying patterns and searching are indeed skills that a developer must aim to improve on.
I did state as many have missed that there is a limit read the entire comment

Thread Thread
 
gunslingor profile image
gunslingor

Great system... but rings my point home too... ive been in many a code base developed by 1000 engineers, where css policies are different throughout precisely due to iteration by each developer individually, without a common folder structure, without a standard... this is the common folly of iterative design, it's equivalent to shoot first and ask questions later, which is definitely appropriate in some scenarios. Imagine an app where the login page uses your standard, the home page uses nested Ultra specific css and the dashboard uses bootstrap... worst case scenario... now imagine everything in the same folder, or even just organized by file type. Plan for the future, plan for architecture... there is a big difference to me between Programming and Software Engineering.

No worries man, take care.

Collapse
 
vildavaettern profile image
Dante • Edited

That's actually not true. If you're working on a giant codebase where there's no organization behind folder/directory structuring or assigned naming conventions, it'll prolong your onboarding process a great, great deal.

Being organized with this kind of stuff means being cost-effective and empathetic to your coworkers.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

you have find in files, your folder structure is irrelevant in this context, that is how as somebody who had no-one to onboard me got around and its battle tested, its true in my context certainly not false.
I have also stated that at a small scale, no structure a flat structure is sufficient at a prototyping phase, adding what you need, when you need it. Im sorry if you disagree but from my experience thats what I have seen.

Thread Thread
 
vildavaettern profile image
Dante

I think its absolutely okay and valid approach if it's a personal project. Then the only pain you'll receive there will be one of your own doing.😆 But when working with others I wouldn't advise others to "not get hung up on structure". I think that kind of mindset is a gateway to start writing legacy code that nobody wants to touch in the future because messy/sticky/non-comprehensive.

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀

My point is iteration is superior to perfection and organisation that will change countless times in the beginning, why don’t you as an experiment write a todo list in a flat structure with no folders, and then explain where the folders start to be needed, that would be an interesting post no?

Thread Thread
 
vildavaettern profile image
Dante

Not really following your thread there. Anyway - all I know is that if I had to work with someone who had zero care in the matter, I'd be worried about the impact that developer's code would have as far as maintainability goes. But you do you! I'm honestly not here to argue. I just felt it was important to point out another perspective. Hope you have a nice day!

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀

I promise there is no argument here I’m also presenting the alternative point of view as well 😊 have a lovely day

Thread Thread
 
vildavaettern profile image
Dante

Absolutely! Which is totally valid as well. Thanks.😊

Thread Thread
 
noruwa profile image
Obaseki Noruwa

I really enjoyed this thread, it is insightful. Learnt alot from you guys perspective.

Collapse
 
tnypxl profile image
tnypxl

I use well-structured folders so I don’t have to rely on find in files.

Collapse
 
gunslingor profile image
gunslingor

Really man, this is old 1980s vim approach. Software architecture is very important, you don't have any of it if all files are flat in a 1000 file application. But even in the vim days we considered architecture. Iteration is actually the least effective method of implementing, short and long term. How would you feel if they built bridges via iteration, why would this be okay for heart monitor software, or even a web-based front-end where personal information can be leaked... there is only one reason, lower upfront cost if you ignore architecture, régulation, new hire onboarding time, comprehension time for when devs modify each other's code, etc, etc.

But when you start a business, actual operating costs far exceeds upfront... so plan for the future and flat file word search ain't it... at least use the IDE click to go feature, faster than typing when tracing and also confirms object is accessible in real time.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Op:

had some mileage before it got too much

80s eh, I’m not that old and I don’t use vim 😂

Collapse
 
meonaisse profile image
Romeo

I dont understand what this mean, can you write in plain english? Thanks

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Bristolian to English translation:

Image description

Collapse
 
devnaqvi profile image
devnaqvi • Edited

Thanks but I disagree with you on keeping the styles in assets directory.

Collapse
 
raythurnevoid profile image
Ray Thurne Void

I like to organize folders per functionality context rather than architecture layer. For instance i can keep only a components and a utile folder, everything else depends on the project. Eg: eg i may have an auth folder for all things related to authentication, or a transformers folder to store all the modules that are used to convert a data type to another or a cart folder for all the modules related to my e-commerce cart logics.

I also like to design my code to minimize the number of call stacks.

Collapse
 
noruwa profile image
Obaseki Noruwa

Folder by functionality is also a great method of structuring web application, Other devs can easily understand your application features 👍🏾 .

Collapse
 
javavista profile image
Javier Carrion

This is the reason I prefer Angular.

Collapse
 
gunslingor profile image
gunslingor

Spent 3 years doing angular, 3 years doing react. They are truely about equal, each has its strengths. As I recall in angular the value attribute connects to the controller, in react its really the onchange event more so, I did like than in angular better. Angular separates view and controller explicitly, react combines and encapsulates them by default, but still easy to externalize. I find react hooks are more pleasant to work with than the angular equivalent, forgot the term, been a while. Also the child composition aspect of react is great when used right. Angular development focuses a lot on separating view and controller, classic mvc approach that is solid, many advantages. React on the other hand focuses on separating the view layout logic from the view logic (e.g. submit)... layout is king in react, Controller is king and View is queen in angular. I have a lot more fun in react but it takes a while to really understand it and use it correctly compared to angular. Angular is kinda like php + js to but with a modern templating engine, react changes the paradigm more... for better or worse depends on the property and application.

Collapse
 
viyash profile image
viyashdoss

same here

Collapse
 
awatansa profile image
awatansa

Seems to me like a good approach initially (means for small sized projects), but very bad on enterprise level projects.

My Point of view is to create a structure more like modules/feature based and keep some common functions/classes or whatever in a separate folder such as common/core.

Collapse
 
leob profile image
leob • Edited

Yeah not bad, very generic though, not every project needs all these folders, and some projects need more/other folders ... another remark is that oftentimes much of the folder structure tends to be more or less 'dictated' by the framework that you're using.

The distinction between views and pages is pretty vague - views are supposed to be "higher level" than components, but "lower level" than pages?

(funny how I'm in fact contradicting myself because in my latest project I have indeed "components", "views" and "pages", so it seems to fulfill a need, lol)

Collapse
 
gunslingor profile image
gunslingor

This is horrendously bad advice. The article is basically recommending organizing by type as the first layer, instead of subject. But this is precisely the reason modern large codebases are so difficult to maintain and are bugy and insecure and cost insaine inappropriate quantities of cash to maintain.

My main advice would be this:

  • generally, 2 top level folders under src, common and pages or routes depending on whether it's front or backend.
  • common folder can be organized by type as the article describes, pages get ordered by page subject and complex pages can have layers of type and subject folder structure. Login is simple, stock trader will be nested deep.
  • ensure that ALL subject based folders (login, email, etc) have only one file in it, referred to as the [subject] container and subfolders for its children and subject specific support files; one must make smart decisions about chosing the substructure here, by type or subject. This tells the dev the root file, instead of having to open 20 flat files and reverse engineering something that was assembled but not engineered. Also ensure all type folders are broken down by subject and generally avoid flat structure here unless it makes sense (e.g. if you have 1000 related forms, flat might be appropriate, until they have their own reducers, css, etc).
  • I would also strongly consider adding another layer: common/data and common/view as well as pages(or routes on backend)/login/(data and view). This significantly improves code in react applications because view should be data independant in react, react has no support for data it is view and layout control only... so data often suffers from the learning curve of react and the view does as well because data ends up encapsulated in the view by those new to react.

When designing architectural structure one must know what one is building. This is rarely the case in the web world were iteration is king, unfortunately. This is why it is key to refactoring and refactoring often with iterative architectural solutions.

But be warned, you do it like this app suggests, and it's a huge complicated app used by millions, it will be a costly nightmare in the long term.

Collapse
 
noruwa profile image
Obaseki Noruwa

Thanks for your comment, I really appreciate it. I will use your advice on web projects from now on 👍🏾.

Collapse
 
awaisalwaisy profile image
Alwaisy al-waisy

feature folder is good to know

Collapse
 
httpjunkie profile image
Eric Bishard

Folders exist to be used. Just like code and tests they should be self explanatory and make understanding discovery and navigation easy. Just at important is naming of the files and co-location of certain logic.

Not one is really more important than the other but folders like layout of your application create a better DX as layout creates a better UX.

It's always interesting to see how others do it, stunt just take everyone's advice, it needs to be a team decision as with most things, but there is some definite and obvious patterns that can be learned through articles such as this.

Collapse
 
szeredaiakos profile image
szeredaiakos

That is functional domain partitioning. It's very good at creating small apps fast. However if extended it completely breaks down on maintenance. Recomended only for less than 30 un-nested screens.

I prefer problem domain. In all cases even if i have to write more code.

Collapse
 
spock123 profile image
Lars Rye Jeppesen

This is basically about React based structure. For everybody else, look elsewhere.

Collapse
 
kennedykeinstein profile image
KENNEDY

Very Useful!

Collapse
 
shubhsharma19 profile image
Shubh Sharma

Great share!

Collapse
 
erickwilson371 profile image
answer buzz

That's in addition a great publish which i really valued learning. It's not every day that people maintain the opportunity to discover something.

Collapse
 
codearct profile image
Mehmet Akif Yıldız

I wish i could see whole folder structure.

Collapse
 
tonynyagah profile image
Antony Nyagah

This has come just in time for me. Was starting on a Nuxt 3 project and the setup command doesn't generate an folders 😅

Collapse
 
lizardoct profile image
Nene

i'm not sure about styles, but its ok,

Collapse
 
sohanemon profile image
Sohanur Rahman Emon

What about lib folder?

Collapse
 
noruwa profile image
Obaseki Noruwa

The lib folder comes in handy when you have dependencies/packages you are to use in your web application, Packages such as Animate On Scroll (AOS), Chart.js, Axios for external API calls. You create individual files for them, import the package and then export the required code for different pages. In general the lib folder is a place to store supporting code for your web application.

Collapse
 
kyle_felder_b2a745f386011 profile image
Kyle Felder

thanks for putting this up here i used to ask this question on how it’s structured in my coding bootcamp courses never got an explanation

Collapse
 
noruwa profile image
Obaseki Noruwa

I'm glad you found some of your questions answered in this post.

Collapse
 
selique profile image
Moisés Lopes Ferreira

atomic design brad frost see example on arc.js.org/

Collapse
 
konhi profile image
Jan Szymański

that's cool, but i prefer "move the files around untill it feels right"

Collapse
 
chaykov profile image
Chaykov

interesting, it is what I wanted to know :)

Collapse
 
chasi profile image
Wayne "Chasi" Godo

I've been needing this. Thank you!

Collapse
 
quocbahuynh profile image
Quoc Huynh Website

Unfortunately, it's very bad when you are still using pure JS in a React project. In the real-world, we will work with a lot of developers, and each developer will be responsible for write a user story. You should put all related components into a folder.

Collapse
 
henningholm profile image
Henning • Edited

To me this seems like a nightmare arcitecture. My experience is that on most projects there are people comming and going, many with limited experience.

By using a more horisontal arctecture projects will quickly become very messy, and if some people dont follow it correct, (which is going to happen), it will create a lot of confusion. As uncle Bob says in one of his talks => you should be able to somehow visualize the app by looking at the folder structure. Vertical/feature based arcitecture pretty much always better in my opinion... As we all need to take some shortcuts once in a while, as long as the shortcut only affect the feature your building, there is no issue because of the encaptulation. For me this description is very much the oposite of encaptulation which one of the main reason why our job as developers can be a nightmare.