I’ll be the first to admit that I’m an organizational freak when it comes to code. Yet, every time I work with a NodeJS based project, I end up frustrated that I can’t organize my code in a cleaner fashion. NodeJS’s require system completely isolates every file from one another, so to speak, so I always find myself tempted to write in a single-file monolithic fashion that makes my eye twich in self-restraint.
I’ve been putting together a base class that many of my Coffeescript classes extend in order to aid organization. It was kickstarted by The Little Book on Coffeescript, and I’ve continued to add more methods to it. While this problem and solution isn’t exclusive to Coffeescript, its syntax and class construct allows for an incredibly clean solution. This solution also isn’t exclusive to NodeJS, but I find it’s the most practical and helpful in that environment.
If you’re a Ruby user, than a lot of this might look familiar to you. So what does the Module class allow us to do? This code is taken directly from my node-activerecord project:
This allows me to cleanly and easily organize the majority of the code for the Model class in other files. The Module class also allows you to do things like:
I’ve setup a repository for the Module class if you would like to contribute. The project is also on NPM, and if you would like an easy way to use it in your own NodeJS project simply run npm install coffeescript-module.