Ruby DCamp 2010 Session 3: How to read and understand code better
Ashish
Knowing the difference between good and bad code is important. (anti-patterns are useful to be able to identify)
Ruby has many different options for object composition, so it can be hard to read many different types.
It isn’t about knowing what is elegant or not, it is about understanding what any given code is doing.
irb is very useful for understanding what a piece of code does. With irb you and cut and paste code and see it run.
When trying to understand Rails code, start with the models and the routes.
- The routes tell you about the controller actions
- The model tells you about the underlining functionality of the app
Reading tests for a project can help you understand what supposes to do, in the words of the developer themselves.
If a function that you are trying to understand does NOT have a test, or is poorly commented, try writing a test for it yourself.
- Writing tests for functions is a great way to understand a function AND help improve the code base.
Check out Seven languages in seven weeks. It is an interesting dive that can help you understand all languages better.
Don’t understand what a gem does? Open it up with: `bundler open gem-name`
JavaScript: The Good Parts - This is a great book to teach you about JavaScript
Design patterns are language smells (but they are useful for understanding other’s code)
What is an anti-pattern? An anti-pattern is some poorly written code.
Some of the people here think that any comments are an anti-pattern. (I disagree)
- They believe that all code should be self-explanitory
- Write tests instead of comments, they verify what you mean to do.
- Comments are a symptom that you need to fix
Check out metric-fu, it is a good way to help detect code smells
As you get better with git, you will find that the history becomes more important to you.
If you want to understand the best, try to find a mentor. And your mentor is not always an expert. An expert is different than a teacher. Having your mentor co-located with you helps, but IRC can help, too.
- RailsBridge has a Rails mentor program that can help you out.
- There are a LOT of options, IF you want to pay for them (e.g., The Compleat Rubyist)
- A mentor is someone that will give you direction, not just instruction
The ruby world is interesting, since rubyists tend to stress software craftsmanship.
Ruby is now like Java was 15-20 years ago. The developers are excited and energized. As Ruby grows we might see it become an enterprise tool and the cutting edge groups move to other languages.

