→ class << self is harmful

05 February 2008

Please stop using it. You know who you are and what I’m talking about.

I’ve never seen code improved by its use. It adds more lines and less clarity, with no demonstratable advantages.

In larger codebases it makes it difficult to instantly know what kind of methods you’re looking at. Is this a group of class methods or a group of instance methods? Do you seriously expect me to know the context based on indentation? What is this, Python?

I used to love it at first too, don’t get wrong. It’s a super rad Ruby trick. It’s helpful for making private class methods or for setting class instance methods. But for the general use case (creating a bunch of class methods), it’s just unneeded.

No, def self.blah is not “un-DRY.” In fact, it makes refactoring easier. Try it out. I promise it’s cool.