One Kata and a lot of tricks
In our last coding dojo, instead of a randori we had a kata prepared by Danilo Sato. He solved the Minesweeper problem in ruby in about one hour using proper TDD and stopping for questions/suggestions from the audience. Here are some of the tricks people could learn from that session:
- When working with ruby on a Mac, the RSpec bundle for TextMate is great. It has really helpful shortcuts and can easily run your specs and see your reports inside the editor.
- Rspec can be used as a TO-DO list during the development of a class. This can be done by creating pending tests as reminders of test cases to be covered. These are tests appear "yellow" state, waiting for a proper implementation.
- Autospec is also a great tool for TDD. Seeing a test becoming green is fun, but doing so without running it manually is way better. Specially using Growl.
- When doing a presentation using the command line, it's easy to be tricked by font colors. They may look great on screen, but a projector may not like them.
- During TDD, it's really important to know how to fake it. But knowing when to refactor the fake code is even more.
- Being able to create methods like "[]" in ruby can be really helpful.
- TextMate allows you to run any text file or even just a few lines of it as a ruby script . This can a good alternative to play with the language instead of using irb.
- A piece of code can sometimes be used as a good visualization tool for a problem. Here's an example:
it "should find neighbours around cell" do
@field.neighbours_at(1, 1).should have(8).neighbours
@field.neighbours_at(1, 1).should include([0, 0], [0, 1], [0, 2],
[1, 0], [1, 2],
[2, 0], [2, 1], [2, 2])
end- It's easy to create bugs by using "..." instead of ".." in ruby ranges.
- Multiple assignments FTW.
- jUnit should have something like nested describes in RSpec. It's a great way to group related tests for a single class.
- Nice RSpec reports can be generated even outside TextMate by using "rspec --format html"
- In BDD sometimes makes more sense having multiple assertions inside the same test. The condition in this case is that they should all help to verify a single behavior of the class being tested.
- Being able to refactor a field into a method without having to care about the references to that field is really cool.
- Assertions for boolean values in rspec are very elegant.
- The pending feature in rspec can really good for big refactorings. It helps to be sure a test or more should be failing during the process.
- Creating your own matchers can make a rspec test more readable. And people should use this feature from jUnit more too.
- It's easy to get confused by the use of &block and yeld in ruby. In any case, it's better to learn both and stick to only one of them whenever possible.
- Some ruby conventions are really smart. Append "?" to describe a boolean method, or "!" to do the same for a method which changes the internal state of the class.