Helpful Rails book: Rails AntiPatterns, by Pytel and Saleh by jgn on Tuesday, May 24, 2011 in Reading, Rails, Reviews, Ruby, and Code

So I'm reading Chad Pytel and Tammer Saleh's book Rails AntiPatterns: Best Practice Ruby on Rails Refactoring, and it's a good one. Oops, but I got the title wrong: It's RailsTM AntiPatterns: Best Practice Ruby on RailsTM Refactoring. (I don't want to get sued for leaving out those TMs.)

I've been writing RailsTM code for a long time, and this book is embarrassing to read. Honestly, after reading a couple of chapters I cranked up my editor and made some tweaks to my code. Whew. Truly, this book has the goods on any number of things you're doing wrong with your RailsTM app, and I would advise you to take seriously their advice.

There are chapters that teach things you should already know (on Models, Views, and Controllers), but also chapters that go beyond (those on using third-party code and services), and some where I think these guys know more than just about anyone (the chapter on testing). I kinda wish the testing chapter didn't use Shoulda, but I guess it's OK, since they invented it.

It's hard to find things to gripe about. One oddity is that they recommend HTTP 401 for application-level (i.e., controller) authorization errors (p. 205): But I thought 401 was reserved for specifically HTTP-related auth (e.g., basic or digest - see the RFC). I guess you can use it like this. They also talk about denormalization, and replacing joins with column constants (pp. 79-82). I guess so. (To be sure, they say that if there's going to be a UI, you do want the table.) I think increasingly we need to look at our schemas as though a non-Rails application will be using it, sooner rather than later: In which case, such constants really do need to be in a separate table, even if there is no UI. So I would advocate sucking in a table with the right codes, putting the key values into the table, and just leaving out the association if you want to avoid the joins. Also in this area, they put their constant values into an Array (p. 81). Dudes, use a Set. Please.

These manufactured gripes aside, it's a lovely book. Now let me get back to my editor and my RailsTM app so I can remove some more embarrassments. Sheesh, Law of Demeter AGAIN!?

comments powered by Disqus