Refactoring IncludeGoogleJs

I’ve just wrapped up refactoring my IncludeGoogleJs plugin for Rails, and what a fun few days the process was.

This was the first plugin I ever wrote (the only plugin I’ve written so far, actually), and it had been over a year since I first released it. Going back over it this past week, there were chunks of code that I didn’t understand anymore, plus functions trying to do way too much without being clear about what was happening. I also added unit tests galore (more lines of test code than lib code!) and a series of rake tasks for getting the JS libraries and cleaning them back up for testing.

Cleaning The Code

I’ve just started reading Clean Code, and I’ve been talking with Brian Hogan about it quite a bit as he’s been reading it, so I’ve tried to take the nuggets I’ve learned from those places to heart as I did this refactoring. The new version doesn’t have the mess of class variables at the beginning, though there are still a few.

Most importantly, this new version is understandable. I have method names that actually describe what’s happening, and methods that are much simpler to understand at a glance. No more ridiculous chains of functions, causing untold pain as they slowly unravel.

New Features

One of the nicest things about doing this rewrite, besides the challenge and the learning experience, was that it’s so easy to add in new features now! As I worked through the code, I got the idea to handle library dependencies, so if you include Scriptaculous through the javascript_include_tag, it’ll make sure that Prototype is ready and loaded first. I’m doing the same with jQuery and jQuery UI. This is a very minor feature, but one that I couldn’t imagine trying to add in the old version of the code.

Try IncludeGoogleJs

So why don’t you grab IncludeGoogleJs and give it a shot? Put it through the paces, break it, and let me know what I missed. Or maybe it’ll help lighten the load on your server, and your users, a bit. I’d love to hear about that too.