[rspec-users] class << self considered harmful... really?

Student blogger at pierian-spring.net
Thu Nov 27 12:50:35 EST 2008


Everyone seems to agree on the basic issues, but are coming at it from
different viewpoints.

1) If you have a bunch of class methods, you have a problem.
(class#File:  I mean you!)
2) Maintainability is paramount.

The question, then, should be: which of these patterns (class << self
or self.method) encourages/discourages good behaviors?

As I've hinted, Ruby itself is a bad example.  So is Rails.  I'm
beginning to wonder, however, if this is not necessarily the case for
languages and large frameworks.

What class is this?  The detractors of class << self appear to me to
be ignoring the fact that many files have multiple classes defined
therein.  If I'm looking at a group of methods in a file, I NEVER know
which class I'm in until I search backwards for the "class" token.
(So keep the token "class" out of your comments & docs)  self.method
doesn't do much to help this.

But XP extremists argue that a class should never be bigger than a
couple of pages in the first place.  Maybe that is the real problem.

I prefer class << self as it seems to aid refactoring and looks
cleaner.

YMMV


More information about the rspec-users mailing list