views:

258

answers:

9

When you start working on an existing Rails project what are the steps that you take to understand the code? Where do you start? What do you use to get a high level view before drilling down into the controllers, models, helpers and views? Do you have any specific techniques, tricks or tools that help speed up the process?

Please don't respond with "Learn Rails & Ruby" (like one of the responses the last guy who asked this got - he also didn't get much response to his question so I thought I would ask again and prompt a bit more). I'm pretty comfortable with my own code. It's sorting other people's that does my head in and takes me a long time to grok.

+1  A: 

I would say take a look at the tests (or specs if the project uses RSpec) to get an idea at the high-level of what the application is supposed to do. Once you understand from the top level how the models/views/controllers are expected to behave, you can drill into the implementations.

Wayne M
+1  A: 

If the Rails project is in a somewhat stable state than I have always been a big fan of using the debugger to help navigate the code base. I'll fire up the browser and begin interacting with the app then target some piece of functionality and set a breakpoint at the beginning of the associated function. With that in place I just study the parameters going into the function and the value being returned to get a better understanding of what's going on. Once you get comfortable you can modify the functionality a little bit to ensure you understand what's going on. Just performing some static analysis on the code can be cumbersome! Good luck!

toddk
+4  A: 

First I use the app, noting the interesting controller and action names.

Then I start reading the code for these controllers, and for the relevant models when necessary. Views are usually less important.

Can Berk Güder
+4  A: 

Look at the models. If the app was written well, this should give you a picture of its domain model, which is where the interesting logic should live. I also look at the tests for the models.

The way that the controllers/views were implemented should be apparent just by using the Rails app and observing the URLs.

Unfortunately, there are many occasions where too much logic lives in controllers and even views. That means you'll have to take a look into those directories too. Doubley-unfortunate, tests for these layers tend to be much less clear.

nakajima
+1  A: 

I can think of two reasons to be looking at an existing app with which I have no previous involvement: I need to make a change or I want to understand one or more aspects because I'm considering using them as input to changes I'm considering making to another app. I include reading-for-education/enlightenment in that second case.

A real benefit of the MVC pattern in particular, and many web apps in general is that they are fairly easily split into request/response pairs, which can to some extent be comprehended in isolation. So you can start with a single interaction and grow your understanding out from that.

When needing to modify or extend existing code, I should have a good idea of what the first change will be - if not then I probably shouldn't be fooling with the code yet! In a Rails app, the change is most likely to involve view, model or a combination of both and I should be able to identify the relevant items fairly quickly. If there are tests, I check that they run, then attempt to write a test that exposes the missing functionality and away we go. If there are no tests then it's a bit trickier - I'm going to worry that I might inadvertently break something: I'd consider adding tests to give myself a more confidence, which will in turn start to build some understanding of the area under study. I should fairly quickly be able to get into a red-green-refactor loop, picking up speed as I learn my way around.

Mike Woodhouse
+1  A: 

Run the tests. :-)

If you're lucky it'll have been built on RSpec, and that'll describe the behavior regardless of the implementation.

A: 

Look at the documentation, there is pretty good documentation on some projects. It's a little bit hard to understand other's code, but try it...Read the code ;-)

matiasinsaurralde
A: 
  1. I run rake test in a terminal
  2. If the environment does not load, I take a look at the stack trace to figure out what's going on, and then I fix it so that the environment loads and run the tests again
  3. I boot the server and open the app in a browser. Clicking around.
  4. Start working with the tasks at hand.
  5. If the code rocks, I'm happy. If the code sucks, I hurt it for fun and profit.
August Lilleaas
A: 

Aside from the already posted tips of running specs, and decomposing the MVC, I also like:

rake routes

as another way to get a high-level view of all the routes into the app

./script/console

The rails irb console is still my favorite way to inspect models and model methods. Grab a few records and work with them in irb. I know it helps me during development and test.

AdamK