[rspec-users] class << self considered harmful... really?
brian.takita at gmail.com
Thu Nov 27 20:12:55 EST 2008
On Thu, Nov 27, 2008 at 7:58 PM, Mark Wilden <mark at mwilden.com> wrote:
> On Thu, Nov 27, 2008 at 4:40 PM, Brian Takita <brian.takita at gmail.com>
>> When maintaining code, I often wish I could travel back in time and
>> tell the writer "Don't do that". :)
> I don't see how we can solve the problem of people not following style
> guidelines by giving them another style guideline to follow. :)
Honestly, I'm not going to get my panties in a bunch if I see code
with def self.method_name in it. I don't care what you do with your
code, unless I need to maintain it.
If there are problems with the code, I'll consolidate these methods
into a class << self block so I can have a better understand over what
is going on.
I think that if we were going to follow a style guideline, class <<
self is more consistent. It leaves less decisions to the programmer,
less opportunity to make things messy, and is more powerful. Some
people have a harder time grasping nesting and some people have a
harder time with less consistent code. Choose your poison.
>> module methods have encapsulated state.
> Probably 99% of the (non-framework) class methods I've seen have no state.
Thats interesting. I can't give you a percentage, but I often either
see state, class methods referencing other class methods (which gets
very confusing with def self.method_name btw) and passing along the
>> We all have seen YAGNI
>> bite us in the ass too, especially when it means turning off the
>> brain. I don't think 2 extra filler lines of code make that strong of
>> an argument, when there is more than one class method (and even if
>> there is one class method IMO).
> If writing those two lines of code are unnecessary for any of the uses you
> and others have described, I would call that turning off the brain. :)
I just explained that its a trade-off, so you can choose to ignore the
trade-off. I'll let you think about that one.
I suppose that we can write our entire program with one LOC. I suppose
thats following YAGNI. Hell, why use do end? Its two extra lines of
code all over the place.
We can also remove lines of code by using ; everywhere. No more
newlines. Awesome :)
>> I think we also like how consistent conventions and delineation of
>> responsibility make code faster to read and understand.
> Agreed, but I think this begs the question under discussion.
I don't follow. Can you clarify?
> (For those who are tired of this non-RSpec-related discussion (but
> apparently find themselves forced to read it), I really will try to wrap up
> my contribution. I don't have the time for the Ruby list, so this is the
> only place I have to talk about fun stuff like this. ObRSpec: the discussion
> did illuminate a situation where tests can pass but the development app can
> fail - when using class variables in RoR.)
Yeah, state in class
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users