Hot Koehls
  • Email
  • Feedburner
  • Linkedin
  • Twitter
  • Home
  • About
  • Archives
  • Contact
  • Software
    • S3imple Backup
    • Twitter Feed Archiver
    • FileTime
    • Flickr API Demo
Search
Home» For techies » Writing Modular Code – Overrides

Writing Modular Code – Overrides

Posted by Frank - April 5, 2008 - For techies
4

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).

programming, zen cart

4 comments on “Writing Modular Code – Overrides”

  1. Gary says:
    April 9, 2008 at 10:27 am

    Daddy like.. Well done.

  2. Mark says:
    April 11, 2008 at 9:43 am

    Being able to articulate this is sometimes just as important as actually doing it to be able to kick it off. Nice post.

  3. owen says:
    May 23, 2008 at 11:11 am

    All of this is basically a howto on building a extension framework. you have function overrides, query overrides, onunload, before, after, when, listeners, observers, lookers, triggers, catches

    I look at this and I say why does this look like your writing a framework? overrides really have nothing to do with modular code. Modular code is for “simplicity” not “flexiblity”. its important that you separate whats a “feature”/extension/option/”what if” from stuff thats actually a part of the software requirements.

  4. Frank says:
    May 28, 2008 at 5:41 am

    @owen: I agree with you 100% that overrides play a huge part in frameworks, but why don’t/can’t they apply to modular code in general? If I’m writing homebrew, sans a framework, I can still put override logic in place so that other developers (or even my future self) can modify or extend my work with minimal effort. We hack code because we have to, but even homebrew code can be written to avoid this generally bad practice.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Categories

  • For entrepreneurs
  • For everyone
  • For techies

Latest Tweets

  • The word traps planners plan themselves into | Life. Then strategy http://t.co/iANAdASb
    May 8, 2012 - 2:43 pm
  • Random network security tip for those about to appear on TV - Boing Boing http://t.co/tC1lXFQ4
    May 8, 2012 - 1:42 pm
  • A Picture http://t.co/H846Uy69
    April 27, 2012 - 12:25 pm
  • The Broken "Buy-One, Give-One" Model: 3 Ways to Save Toms Shoes | Co.Exist: World changing ideas and innovation http://t.co/RI0sVMW6
    April 10, 2012 - 12:23 pm

Recent Comments

  • whiz on What 255 characters looks like
  • Andrew on Find the second (or third, or fourth) occurence in a string
  • IanArcher on Get number of message parts in an email using PHP
  • Usama on Remove parent directories from tar archives
  • Frank on It’s dangerous to go alone

Recent Posts

  • It’s dangerous to go alone
  • Create Self-Signed Wildcard SSL Certificate
  • What comes after the yottabyte?
  • Write code like they do in Hollywood
  • Brian Rolle machine gun celebration
(c) 2012 Frank Koehl. All Rights Reserved.
  • Contact Us
  • Sitemap