Writing Modular Code - Overrides

If you code long enough, you’ll eventually hear that you should write your code in a modular fashion. Problem is, the ivory tower sage who told you to do that can’t communicate beyond the proverb. So starting today, over a few posts, I’ll lay out in hardcore concrete terms what the heck it actually means to write modular code. People usually start trying to explain modular code from the perspective of object oriented design, or “OOP.” We’ll come back to that one later, and start with an even simpler concept: overrides. As the term implies, overrides in code occur when a system allows the introduction of third party code into the logic flow. Essentially, at a given point, the code stops to check if there’s a unique set of instructions to run, and fall back on its defaults if none are found. Done right, the potential for introducing an override can occur at every point the flow of logic in your program “turns” (e.g. load this library, turn and load that DB data, turn and set up page headers, etc). Overrides come in two basic flavors: file existence checks and runtime configurations.  Here’s a simple example of each. * File existence check

Your web project stores all the css definitions in a folder called “stylesheets.” Within that folder you have another one called “overrides.” When your stylesheets are loaded, your code first checks to see if a file with the same name exists in the overrides folder. If a match is found, the file located in /stylesheets/overrides will be loaded instead of the one found in /stylesheets. * **Runtime configurations

**Let’s say that your /stylesheets folder contains all the css for your entire site, but you only need 1 or 2 on a given page. You don’t want to blindly load all of them on every page, because that would kill your load times, and the styles might conflict with each other. Instead, your code has a single default CSS in mind, but first it checks for a variable called load_custom_css. That variable is defined in a configuration file unique to every page on your site. If that variable points to alternative css files, those are loaded instead of the default.

In the real world, both methods are typically used together. We can combine our two examples above without too much effort: after the custom CSS files are selected from load_custom_css, the code can do a quick check in /stylesheets/overrides to see if that file has a replacement. The open source world provides a plethora of ultra high quality examples, because they have to account for upgrades, and end users make modifications. In Wordpress, just drop some properly formatted code into the wp-content/plugins folder, and the system will recognize and execute it properly. The Zen Cart e-commerce system allows developers to get even further into the nitty-gritty with its InitSystem and Observer class, which control how backend libraries and overall page processing occurs.  Those are relatively new features on top of a robust templating system; a template designer is required to include only the files needed to achieve the desired layout. If a necessary file is not included in a custom template, the system pulls the file from the default template location. Overrides are important for any software project because they allow outside developers to hook into your system without having to hack up your core. The importance to an open source project is obvious (see above), however don’t discount their utility if you maintain an internal company system, even if you’re the only developer (which is really not a good idea, FYI). * You will not be the only developer forever

You will get promoted, or find a better paying job, or someone will get hired to help you. Change is the only constant, especially in this line of work. You’d be wise to prepare your system for that eventuality, and make the new guy’s transition easier. * Even if you were, you still can’t do it all

Most companies doing their own internal development will hire outside developers at some point. They complete portions of a larger project, speeding it up, or build functionality for which a company has no internal specialty. You can’t do it all, so make your own life (and the life of that outside dev) easier by allowing him to link into the software easily.

With overrides, any and all foreign code is corralled into expected locations, allowing both you and the 3rd party or parties to manage your own areas effectively. Do it right and you’re on your way to modular code (note: doing it right comes up later in the series).