zerosum dirt(nap)

evolution through a series of accidents

zerosum dirt(nap)

Why Namespaces Are Important

April 17, 2009 by nap · Comments

Apparently both Extlib and the Mash library, which the latest version of the Twitter gem relies on, both define Mash as a top-level class. This is lame.

It means that if you're building an app that uses the Twitter gem to poll their API, you can't use DataMapper (which relies on Extlib) for your ORM. This is but one example of an affected application, of course (retweet stack trace). Integrity's twitter notification support is another victim. Epic namespacing fail.

So whose fault is this? The Mash library? Extlib? Which came first? Which is more widely used? OMFG fight!

Seriously, who cares? If you're asking yourself these questions you're missing the point.

module Org
  module Zerosum
    module Util
      class CollisionPrevention
        def initialize
          puts "gtfo"
        end
      end
    end
  end
end

Java users, and consumers of other modern programming languages, figured out why namespacing was important a long time ago. In fact, one of the few things I actually liked from the Java world was it's use of hierarchal domain names in packages to ensure class name uniqueness and prevent this sort of ugly namespace collision. It's simple and effective. And yet we don't see a strategy like this in use in Ruby gems all that often. It's not like Ruby doesn't provide us with a mechanism for it; Modules provide a really easy way to accomplish this. So why aren't we taking advantage of it in our libraries?

I don't mean to single out Extlib and Mash in my rant here -- I'm just picking on them because they happen to demonstrate an obvious problem that we've all run into on occasion. Hell, I'm guilty of it too. Anyway, the bottom line is, that if you intend your code to be sucked into someone else's application and reused, please do your best to prevent obvious namespace collisions. It's as easy as A::B::C (or Tld::Domain::Feature::Specification).

Thanks for listening! :)

blog comments powered by Disqus