|
|
|
|
|
by urbanautomaton
4952 days ago
|
|
> I think that we agree on the bottom-line: UserContentSpamChecker has no need for a state and is just some kind of namespace. I'm not sure that we do. I agree that the current implementation of `UserContentSpamChecker#is_spam?` doesn't need state, but I don't agree with your conclusion that this distinction should be made obvious to clients, who surely just care that they have a thing that will check for spam, to which they can pass content. They don't care if it's stateful or not, as long as it accepts strings and returns booleans. Why are we trying to tell them that this particular method is stateless? After all, even that offers a false guarantee. In your implementation, `#is_spam?` is just another method on an object instance - in this case an instance of class Module, referred to by the global constant UserContentSpamChecker. I can happily use instance variables in such a method: module Stateless
extend self
def no_state_here!
@thing ||= 0
@thing += 1
end
end
> Stateless.no_state_here!
=> 1
> Stateless.no_state_here!
=> 2
> Stateless.no_state_here!
=> 3
Your implementation resists future refactoring, forces clients to create a hard dependency on a global constant, and doesn't make the guarantee you intend it to convey. We agree that the current implementation doesn't need an object instance, but can you explain what is actually better about avoiding one? Ruby is an object-oriented language, after all; objects are its common currency. It's not like we're introducing the Strategy pattern for a four-line method, we're just using Ruby as she is wrote. :-)p.s. sorry vidarh, I see you've covered some of this already - serves me right for half-composing a reply then wandering off... |
|
Obviously if my future spam checker is going to be introducing a side effect (eg: Akismet) I am going to refactor my code a no longer user a class method. In any case I don't see how it would "resists future refactoring". Grepping for UserContentSpamChecker is not exactly rocket science.