[rspec-users] RSpec makes me want to write better code

Ashley Moran ashley.moran at patchspace.co.uk
Fri Sep 26 07:31:07 EDT 2008

On 25 Sep 2008, at 17:48, Mark Wilden wrote:

> "Each controller action only calls one model method other than an
> initial find or new.".
> I didn't get that article (or, rather, that particular subarticle)  
> at all.

I kinda tuned out when I read, "Polymorphic associations, however, are  

I wouldn't let that guy near a CSV file of my shopping list after  
reading that...

> It sounded to me like some of the virulent REST advocates who want  
> to shoe-horn every action into an HTTP verb. The controller should  
> do what the controller needs to do. If that means displaying just  
> available products instead of all products, it should do that "with  
> intention," not just as the default find action.

Is a better rule "each controller action should contain no more than  
two branches"? (But then, I try to apply that to all methods, and even  
then, I try to push conditional code as far down as possible.)

> That said, scopes can and should be defined, but that's a well- 
> understood idiom of Rails programming and can lead to abuse.
> Would this mean that I should change my custom find naming  
> convention to
> something more general that would never have to be renamed over time
> such as find_for_index, find_for_create, etc?
> That puts too much controller knowledge in the model, in my opinion.

I'd try identify the problem that is being solved in the controller.   
*Why* does ProductController#index need that certain subset of the  
data?  Maybe the model method should be something like:

* Product.top_selling
* Product.not_selling
* Product.needs_updating

Does this better express the intent?

> What matters is that the desired behavior, possibly including and/or  
> implemented by calling find_for_index, is achieved. If such a naming  
> convention was very important to the project, then a spec like that  
> would be appropriate. However, I'd regard it as a code smell if I  
> saw such a test.

I agree (I think).  Conventions decrease the mental effort to make  
something work at the risk of reduced insight.  Certainly if you are  
automatically checking that the code is following conventions, that  
suggests the convention is artificial and unnatural.  You could end up  
trying to map all the business problems onto a very crude technical  



More information about the rspec-users mailing list