How do you read other people’s code?

When you’re starting out, one of the best ways to learn is by looking at code written by more experienced developers; especially when it comes to learning key concepts like “the Rails Way”.

But where do you begin?

It can be a struggle to figure out how a particular app works when you have loads of models and controllers to wade through.

Well, as you might expect, if the app has tests, then they are a great place to start. Especially if the acceptance tests are written in Cucumber or Spinach. You have a definition, written in English, of what the code is supposed to do. You can go through a feature at a time and trace through the code, from test to route to controller to model and back through to the view. If the tests are written in Ruby (either as RSpec or MiniTest) then you’ve got a bit of parsing to do, but you should still be able to get a good idea of how things hang together.

If you dont have tests then an excellent place to start is the schema.rb file. This is a Ruby description of the database structure and from it you should be able to see which models are important and how they relate to each other. You should also be able to get an insight into how cleanly the developer has modelled the application.

Lastly, the routes file is a great place to understand things from the user’s point of view. Hopefully it should use predominantly Restful routing (if it does not then you will have a load of stuff, probably not very well-organised, to sift through by hand). Restful routes will give you an idea of the concepts that are important to the user (as they will be reflected in the URLs) and the nesting will give you some insight into the application structure.

So there you have it; three places to look to get an insight into someone else’s application, tests, schema and routes.

Do you know what to do but not how it works?

Ever wanted to understand why Rails views work the way that they do? Why variables from your controllers are visible inside your views?

Sign up below to get a free 5 part email course on how Ruby on Rails view rendering works and gain a deep understanding of the Rails magic.

We will send you the course, plus the occasional update from this web-site. But no spam, we promise, and it's easy to unsubscribe