Reading code

Posted by Colin A. Bartlett Fri, 18 May 2007 23:12:00 GMT

Believe it or not there was actually a talk about how to read code. There’s more to it then I realized and there were some interesting handy tips presented, although there wasn’t as much actual substance to this talk as I had envisioned. Here’s some tips:

When reading ruby or Rails code beginners often might not even know where to start. Here’s a pretty good track to follow:

  1. Read the URL of the page in question. In simple apps that aren’t using fancy routes, the URL will tell you the action/controller.
  2. Check out the routes.rb file for any fancy route configurations to find the controller/action.
  3. Open the controller and find the method. Read through it.
  4. Look for any filters on the controller. Usually declared at the top. Find them and see if there is stuff running before or after your method.
  5. See what methods on the model are invoked and read through the model method.
  6. Look for callbacks on the model. Stuff like before_create can add functionality that you might not have realized was there.
  7. Application controller and application helper files often contain other stuff you should look through before doing any spelunking.
  8. Seek out observers. Often declared in environment.rb and residing in the models folder, these are callbacks that often attach themselves to multiple models.
  9. Plugins and libraries in /lib are good to look at, too.
Some other tidbits about finding the method you’re looking for:
  1. Check for normally defined methods
  2. Find any method_missing implementation
  3. Grep through files for any metaprogramming like define_method.
Comments

Leave a response

Comments