Hop off the tutorial treadmill with comment-driven development
You want to build a specific Ruby app – something that parses API data, for example. There’s just one problem: you don’t know how to do it in Ruby! So you figure you’ll hit up some tutorials and see how far they get you… which turns out to be not very far.
But you’re motivated, and know you’ll eventually figure it out. But no matter how many exercises you work through, you never seem to learn the things you need to build your parser app.
What if you could figure it out? You could build your parser app, chew through the API data, and get on with your life.
But there’s still so much Ruby you don’t know! But if you could learn just enough Ruby, you’d be set…
Why tutorials don’t help you create Ruby apps
Tutorials cover all kinds of important aspects of programming: how to define methods, use variables, call libraries… but they never go into details that will help you create your own Ruby programs. How could they? You can build an infinite number of programs with Ruby. Tutorials teach you the different high-level pieces you’ll use, but leave you to figure out how to put them together. And putting the pieces together is the hard part, the skill that results in your unique Ruby app.
Hack your way to a working application with comment-driven development
Here’s a technique you can use to create your own Ruby applications, without ever following a single tutorial. It’s called comment-driven development, and it’s simple:
- Write your program using comments only
- Convert each comment to a method call
- If you’ve defined a new method, “implement” it with comments only
- Repeat steps 2 & 3 until you’re done
Of course, you might not know how to convert each comment to a method call. But that’s the trick: you have a specific problem that you want to solve. It might take you a while to find the answer, but once you’ve found it, you’ve made definitive progress. That sure beats mindlessly paging through tutorials hoping you’ll collect enough info to solve problems on your own!
Here’s a simple example of how it might work:
1 2 3 4 5 6 7
# comments_only.rb # read file data.txt # for each line... # split the line based on commas # convert each item to numbers # print the sum
Expressing the solution in comments alone is a huge win! You’ve already done the hard work of thinking through the problem and how to solve it. Now you just have to do the (admittedly tedious) work of converting the comments to code.
Break big, complex problems into tiny, google-able problems
The big win with this approach is that you’ve broken the big problem of “how do I write a parser app?” into small, gooogle-able problems. You can google “ruby read file” and get hints from the official documentation, or see what other programmers have to say. And you can do that for every step of the problem, until you’re done. And end up with something like:
1 2 3 4 5 6 7 8 9 10 11 12 13
# comments_to_code.rb # read file data.txt file = File.read "data.txt" # for each line... file.lines.each do |line| # split the line based on commas items = line.split "," # convert each item to numbers items = items.map(&:to_i) # print the sum puts items.inject(&:+) end
It might take you a while to figure out each line, no doubt about that. But you’d have to figure out how to implement those lines anyway! Why not do a little bit of upfront thinking about your problem, and make your research laser-focused?
The next time you’re tempted to work through another tutorial, give comment-driven development a try. You’ll be creating your own Ruby programs, faster than you can say, “Who needs tutorials?”