We previously discussed how growing an application organically without any sense of architecture could result in an unmaintainable mess of spaghetti code. One of the great things about imposing structure is that it automatically gives predictability (a kind of filing system with in which we immediately know where a particular piece of code should live).
The same applies to the files that make up your application. Certainly, we could put all of our files in the root of the website, mixing CSS, JavaScript, configuration and HTML files in a long alphabetical list, but we'd be losing out on a number of opportunities to keep our application organized. In this chapter, we'll look at:
Structuring your application is like keeping your house in order. You'll know where to find your car keys, and you'll be prepared for unexpected guests.
One of the ways in which code is structured in large applications involves namespacing (the practice of dividing code up by naming identifiers). One namespace could contain everything relating to Ajax, whereas another could contain classes related to mathematics. Programming languages (such as C# and Java) even incorporate namespaces as a first-class language construct to help with code organization.
Separating code from directories based on namespace becomes a sensible extension of this:
A namespace identifier is made up of one or more name tokens, such as "Java" or "Ext", "Ajax" or "Math", separated by a symbol, in most cases a full stop/period. The top level name will be an overarching identifier for the whole package (such as "Ext") and will become less specific as names are added and you drill down into the code base.
The Ext JS source code makes heavy use of this practice to partition UI components, utility classes, and all the other parts of the framework, so let's look at a real example. The GridPanel
component is perhaps one of the most complicated in the framework; a large collection of classes that contribute to features (such as columns, cell editing, selection, and grouping). These work together to create a highly powerful UI widget. Take a look at the following files that make up GridPanel
:
The grid
directory reflects the Ext.grid
namespace. Likewise, the subdirectories are child namespaces with the deepest namespace being Ext.grid.filters.filter
.
The main Panel
and View
classes: Ext.grid.Grid
and Ext.grid.View
respectively are there in the main director. Then, additional pieces of functionality, for example, the Column
class and the various column subclasses are further grouped together in their own subdirectories. We can also see a plugins
directory, which contains a number of grid-specific plugins.
Ext JS actually already has an Ext.plugins
namespace. It contains classes to support the plugin infrastructure as well as plugins that are generic enough to apply across the entire framework. In the event of uncertainty regarding the best place in the code base for a plugin, we might mistakenly have put it in Ext.plugins
. Instead, Ext JS follows best practice and creates a new, more specific namespace underneath Ext.grid
.
Going back to the root of the Ext JS framework, we can see that there are only a few files at the top level. In general, these will be classes that are either responsible for orchestrating other parts of the framework (such as EventManager
or StoreManager
) or classes that are widely reused across the framework (such as Action
or Component
). Any more specific functionality should be namespaced in a suitably specific way.
As a rule of thumb, you can take your inspiration from the organization of the Ext JS framework, though as a framework rather than a full-blown application. It's lacking some of the structural aspects we'll talk about shortly.