Why It’s good to have the directory structure match namespace structure

By | March 2, 2008

Before I dive to deep into a point bullet list as to why this is important let’s first explain how I got to this point. For the past year or so I’ve been working in Java. One of the few languages that I know of that automatically creates a new directory for each package created (similar to C++ namespace). If you don’t pay any attention to it you’d never even know it does this.

But when you want to look at files in the file explorer you might have some trouble finding the right file, and you may find expanding all the directories annoying (I know I did ;)). Especially when you are browsing the SVN or CVS when each folder takes forever to load :D.

As I noted in the first paragraph were over a year further and I must say that I like structuring the folder structure the same as the namespaces in the code. Let me explain why.

Easier to find a class
Though you may not notice it at first, but it is easier to find a class when the directory structure is similar to that in the source code. After all you already know the namespace it’s in. So locating the files becomes a lot easier.

You instantly know what classes belong to which namespace
I don’t know about all off you but I don’t always have my projects open when I’m documenting on trac. So to figure out which classes are part of a namespace I simply open the directory and find out. That way I don’t have to load the entire project every single time.

Moving of classes to other namespaces becomes easier
When you come to the point that you need to refactoring having all classes from a namespace in one directory makes it a lot easier to refactor. No longer do you have to open each file to find out in which namespace it’s located, just look at the directory structure.

This also introduces the downside that you need to move the classes in the SVN or CVS since the namespace has changed, but hey everything has it’s downsides ;).


Let me know if you now any good reasons, or have some strong reasons against.

Leave a Reply