DEV Community

Walter Guevara
Walter Guevara

Posted on

The importance of having good folder structure in your code

Folder structure is something that most developers usually take for granted because often times it is something that is already present when you join a project.

When you start work at a new company, the projects are typically already set up with whatever structure the original developer came up with. If you clone a project from GitHub, then you also don't have to worry about such things.

But if you are starting a project from scratch with zero code, then coming up with correct file structure can be the most important step in the whole thing. And the most difficult as well.

What makes bad structure?

In order to figure out what makes a good file environment, let me first discuss what makes for a bad one. Because there's quite a few things.

The most notable being the lack of any consistency in terms of capitalization and special characters. The following for example isn't an ideal scenario.

__ main

__Src

__Image_Files

__ More-Image-Files

__evenMoreFiles

For one, depending on the OS that you are hosting your files on, some of these might not be valid at all. But assuming that they were valid, they still lack any form or order. If you are going to go lowercase, then ensure every directory is lowercase. If you want to use the underscore as a separator, then avoid having camel casing.

Decide early on and then stick with it through the course of the project.

This will ensure that you won't have to write any kind of special code later on in order to access directories, since they will all be following the same conventions throughout the entire project.

What makes for good structure?

Having a good naming scheme is only the beginning. There's also the issue of having too many deeply nested folders, making it both difficult to reference files in your code and also difficult to find what you are looking for.

This would look something like the following:

__src

______pages

__________accounts

______________registration

Again, this is totally valid and allowable in pretty much every OS. But the real issue happens when you continue to create more and more child directories time after time. Eventually you will end up with hundreds of folders, some with only 1 or 2 files, and a directory structure that a newcomer would easily be lost in.

Remember, you are not the only one that will be looking at the code. Typical convention aims to have a maximum of 3 levels when it comes to child folders. And if you find yourself needing to nest folders 10 layers deep, then maybe there's another issue that needs to be addressed causing that complexity.

And one last point to mention. This is one that I've personally encountered frequently in the corporate world. And that's too many files in a single directory.

The theoretical maximum number of files that you can add to a folder on Windows is around 4 billion. That's alot. And there's a good chance that you will probably never see that limit reached. But you don't need to hit the max to begin to have issues.

Because having even 10,000 files in a folder is a giant headache for a developer. For one, it becomes increasingly slower and slower to load those directories the more data they accumulate. Especially if these are large files. And secondly, it's just hard to find anything inside of them.

I've mainly seen this happen with image folders, dealing with a website that had around 80,000 image files in a single directory. Opening that folder on its native machine was pretty much an impossible task as it would hang for minutes at a time and taking a huge chunk of the performance.

On a few rare occasions, it even took the website down. Mind you this was on an older server, but still.

The solution to this problem is to pretty much split the files into more folders. Not children of each mind you. Just siblings. And that would look something like the following:

__images

__1

__ 2

__3

Essentially, sub directories with the appropriate numeric paging, splitting each folder into a set number of files. You would need to store the numeric value of the directory that the particular file finds itself in.

This only applies if you are hosting the files on your own servers of course. If you are using a cloud storage provider like Amazon S3, you can avoid much of this hassle.

I hope this post gave you an idea of the trouble that folder and file structure can bring, even before you write a single line of code. But it is something that should be planned out and built accordingly for your particular needs. It's pretty much where everything starts.

Top comments (0)