Reading code
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:
- 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.
- Check out the routes.rb file for any fancy route configurations to find the controller/action.
- Open the controller and find the method. Read through it.
- 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.
- See what methods on the model are invoked and read through the model method.
- Look for callbacks on the model. Stuff like before_create can add functionality that you might not have realized was there.
- Application controller and application helper files often contain other stuff you should look through before doing any spelunking.
- Seek out observers. Often declared in environment.rb and residing in the models folder, these are callbacks that often attach themselves to multiple models.
- Plugins and libraries in /lib are good to look at, too.
- Check for normally defined methods
- Find any method_missing implementation
- Grep through files for any metaprogramming like define_method.
