Defining Classes in Modules "Leanly"

Through some personal development efforts and work on Ubercart, I've been confronted lately with how to best use classes in my Drupal modules. For this post, I'm really just curious how others are defining and using classes in their modules and if the "lean" approach I've outlined below is reasonable. (i.e. Is what I'm doing to ensure Drupal loads less code even worthwhile?)

The general idea I'm following is that I want as little unnecessary code to be in my .module file as possible. To that end, I'm following Drupal's core practice of using and files to hold page callbacks. I believe Views does something similar (coupled with some auto-inclusion), and we're using this in some places in Ubercart.

Now I'm doing some experimental development on making something like a shopping cart a class. I've used a method in my FreshBooks module (w.i.p.) where I define the class in a So, in the module I obviously can't use $object = new ClassName();, but instead I create a small function in the .module that includes the and passes the arguments on to the constructor when it creates and returns the object. From there on out it's using an object like normal.

This means I can have a thousand lines of code tucked neatly into a file that needn't be loaded and parsed on every page request... just those where the class is actually needed. It also helps me personally to isolate all this code in a single file when developing/debugging. It seems like this segmentation would provide a performance boost, but I don't know what sort of tests to do to establish that. Should I just assume less code loaded is better? What about the require_once() bits from Rasmus' Drupalcon session?

What about possible pitfalls? Another module can't simply extend my class right off, but it could just as easily include the before trying to do so. Would the hassle and file access outweigh the memory difference? Thoughts?

A Procedural ASCII Universe

I've always been a big fan of ASCII based games. If I sit down to toy with a project, more often than not I'm doing something ASCII. I was never a great artist, but I loved Roguelikes (ADOM being my favorite) and had a mind to write a multi-player Roguelike back in the day.

Anyways, I just wanted to post about one of the coolest ASCII apps I've seen in a while. The name is Project Infiniverse. It procedurally generates universes with star maps and nebulae or planet surfaces with climate regions, vegetation, etc. It's pretty awesome to check out if you have the time and are on Windows.

The apps are programmed in my compiled language of choice, FreeBASIC. I'd love to be able to write algorithms like that. (There's the geek in me speaking. :D)

Legitimating the Developer's "Groove"

I'm not the only one who knows about it...

"It is clear that interruptions are a major cause of low productivity among programmers. Why? The problem is not the time needed to handle the interruptions themselves, but the time needed to get back into the programming problem. Everybody, no matter what they do, face a reorientation time when they return to their work after an interruption. When you are reading a magazine article and look up to answer a question, it takes you longer to read the next paragraph than if you had been reading continuously."

From Are developers people? by Jakob Nielsen.

It was quite an enjoyable little read. I recommend it, even if you just wanna know a little more about how I think. Wink


Well, for the last several weeks, there has been a FreeBASIC game programming competition in the forums at (view thread). I pleaded with my wife and was allowed to work on and submit an entry. I've sacrificed plenty of sleep to work on the project after she's gone to bed and ended up with what I feel is a solid, enjoyable arcade game with enough bonuses and gameplay strategies to provide some replay value. The title is SECTOR SHOCK, and it is your next favorite pasttime.

The restrictions of the competition were you had to use the original graphics and sound files from Space Invaders. I checked out what I had to work with and dove into brainstorming fast... during lulls in the day, over lunch break with Lyle, and while putting Christina to bed. The week before I had been playing with a game programming library called HGE that makes a lot of things easier... graphics, sound, frames, etc... it's just nice. Many thanks go to Dalex for porting HGE to FreeBASIC.

My final product is a top down space shooter that takes advantage of the arrow keys for movement, WASD for firing, and shift/ctrl for special features. The game features 6 types of enemies and their generators (portals), 3 types of weapons, 2 special maneuvers, and 2 special bonus types spread across 25 waves of combat. Each wave gets progressively harder, while your ship gets more lethal. There are a few different level types, and even some health and support resources that must be managed during gameplay. There are some fun particle effects to provide atmosphere, courtesy of the excellent HGE library without which my entry would not be near as good. I assure you the game is very beatable, as I've played through every level myself. Estimated play time is somewhere around 45 minutes to an hour.

I'm hoping to nab first price, which is 100 GBP, but even third prize is a nice bit of spending cash (Christmas presents, anyone?) when the U.S. dollar is so low. }:) I've gotta find more of these European competitions! I've listed my game in the competition thread with a few more details and credits, but I'll also put the screenshots and links up here. To view the other games entered, head to the competition page at FreeBasic Tracker (a FreeBASIC community site I'm developing... or have been for the last 1.5 years... ;)).

Eye candy:

Main menu goodness.

Probing an enemy to charge the ion cannon.

Wave introduction screen.

The final stage! Crazy!

So... what are you waiting for?

Download SECTOR SHOCK today, and post your high scores and best shot rates up here!

*If you have trouble starting this up, it may be your video card doesn't support the 320x240 full screen mode I'm using. You can adjust settings.ini to specify other screen modes, a windowed version, and any number of debug features. You can also use it to warp levels. Wink