Dave Schweisguth in a Bottle

How many meanings of that can you think of?

Archive for the ‘Programming’ Category

Essential reading for the modern programmer

leave a comment »

Software is a uniquely plastic medium, and the practice of software development changes constantly and quickly. All software engineers are self-taught; even the college-trained learn most of their trade on the job. Though we learn by doing and by watching others and studying their work, I’ve found that reading has consistently been the most powerful way I can understand what I’m doing and learn new methods. So I’m constantly recommending to colleagues one book or another, hoping that they’ll get from it the same value that I did.

To make that easier, here’s a list I’ve been meaning to put together for a long time, of books and other writings that have been essential in shaping my practice of programming. Well-read programmers will find few surprises here, but can be sure that any of these that they’ve missed will be worth their time.

Read the rest of this entry »

Advertisements

Written by dschweisguth

July 16, 2011 at 23:04

Posted in Programming

Upgrading GWW from Ruby 1.8.7 to Ruby 1.9.2 and RVM

leave a comment »

Having already upgraded GWW from Rails 1 to Rails 2 and then Rails 3, the last step to bring it fully up to date was to upgrade to Ruby 1.9.2. It wasn’t difficult, but it took enough puzzling out to be worth writing down, and I even found a regression in Ruby.

Read the rest of this entry »

Written by dschweisguth

June 3, 2011 at 16:12

Posted in Programming, Rails, Ruby

Succinct specs for Rails named routes

leave a comment »

I like all of my routes tested, and I like all of my routes named, and I like all of my named routes tested. I even like to test my RESTful routes; even though it feels a bit like testing Rails, it more than paid for itself when migrating to Rails 3. How, then, to spec a named route succinctly?

Read the rest of this entry »

Written by dschweisguth

May 11, 2011 at 08:48

Posted in Programming, Rails, Ruby, Testing

Rails’ named routes considered helpful

leave a comment »

The current Rails best practice is to use RESTful routes. One nice thing about a RESTful route is that it comes with a name. Similarly, :member and :collection routes are automatically named. But sometimes you need a plain old arbitrary route, which you can then name or not as you wish. It may seem like overkill to give every route a name, especially if the route is referred to infrequently in templates. But there’s another place where you’re virtually guaranteed to need that named route again: in tests.

Read the rest of this entry »

Written by dschweisguth

May 11, 2011 at 08:36

Posted in Programming, Rails, Ruby, Testing

Do dynamic languages need more tests than static languages?

leave a comment »

Recently I made an error in a Ruby program (a Rails application) that I wouldn’t have been able to make in a Java program. I renamed a model method manually — because another class had a method with the same name, and RubyMine’s Rename Method refactoring would have renamed both methods — and although I renamed the method itself and uses of the method in the model object’s spec, I missed uses of the method in a controller and mocks of it in the controller’s spec. All of the tests passed, but the application broke. “Gee,” I thought, “if I’d had to compile everything first, that couldn’t have happened.”

Read the rest of this entry »

Written by dschweisguth

May 2, 2011 at 18:09

Posted in Programming, Testing

Avoiding extra queries in ActiveRecord 3

leave a comment »

ActiveRecord 3 introduces a new query interface based on Arel. The new query methods are more succinct than the old :conditions etc. syntax, and they’re easier to combine into complex queries. Scopes (formerly known as named scopes) are still present and useful, but there’s much less difference between a scope and a simple method that returns a query than there was before. Along with these carrots encouraging you to rewrite all your queries there is the stick of deprecation of the old syntax in Rails 3.1 and removal in Rails 3.2, so most people migrate to the new interface when they migrate to Rails 3.

Migrating GWW to the new interface went smoothly, but when I was reviewing some pages for performance recently I noticed ActiveRecord issuing some extra queries that I didn’t expect, and that weren’t necessary. Fortunately, once noticed, they were easy to eliminate.

Read the rest of this entry »

Written by dschweisguth

May 1, 2011 at 10:33

Posted in Programming, Rails, Ruby

Upgrading GWW from Rails 2, RSpec 1 and prototype to Rails 3, RSpec 2 and jQuery

leave a comment »

GWW originated on Rails 1; I upgraded it to Rails 2 last year. I’d been putting off the upgrade to Rails 3 until a bug in Phusion Passenger which breaks redirects in Apache httpd was fixed, but I want to use a database adapter which supports MySQL spatial features, and that requires Rails 3. I said goodbye to page caching until (crossing fingers) Passenger 3.0.6, made a branch and started editing my Gemfile.

Although Rails 3 has been released for more than half a year, upgrading involved many hiccups and a couple of landslides. I’d have been happy to just update my gems and be done with it, but I had to hunt around enough that it seemed worth logging what I did in case it helped someone else. I’ve kept it as short and to-the-point as I could. Although I fixed all of the deprecations that Rails 3 reported as I encountered them, I won’t mention them further, since in every case they were completely clear and fixing them only required following instructions. I’ve also left out a handful of odd little incorrectnesses in GWW which worked in Rails 2 but broke in Rails 3, since the details probably won’t interest anyone else and the fixes were obvious. Otherwise, here’s how the upgrade went.

Read the rest of this entry »

Written by dschweisguth

April 4, 2011 at 08:29

Posted in Programming, Rails, Ruby, Testing