Saturday, February 02, 2008

Ruby: Did TDD make Ruby a viable option?

Some people ask if Test Driven Development (TDD) is what caused Ruby to become popular. Other people smarter than me claim that TDD is in fact the reason that dynamic languages are now viable options.

I'm sorry, but I disagree.

Can you write a non-trivial application in Ruby without tests and have confidence in it? No. Can you write a non-trivial application in Java without tests and have confidence in it? Again, no.

A compiler lets you know that some things are correct, but should not give you confidence that your application behaves as expected. A trivial example is, I need to know that the calculate_tax (that's calculateTax for you Java fans) method returns 56, not that the result is a Fixnum (or Integer in Java).

As much as it should be, TDD is not mainstream. There's no TDD Conference, but RailsConf sells out every year.

I would actually credit David Heinemeier Hansson with making Ruby viable by creating a framework that drove mass adoption. But, I bet of you looked at the majority of Rails applications you would find empty test folders (or only the generated tests, which are never run). I'm quite sure that's true because I expect the conferences to attract the best of the Ruby developers, and several of the people I talk to at those conferences "simply don't have time" to write tests.

10 comments:

  1. Have you ever tried a functional programming language? There when the program compiles it usually works ok.

    Of course there still maybe errors but the point is that you need tests to find errors in ruby programms, whereas compiling a program in a statically typed language will find quite a few. That is the reason why it is crucial (more important than in statically typed language) to have tests when programming in ruby.

    Don't get me wrong, I would still have tests when programming in a functional programming language.

    ReplyDelete
  2. Anonymous7:27 PM

    @jay - I'd go the complete opposite direction and say "Ruby made TDD a viable option". No need to build Java classes + test classes just to run a small test iteration.

    ReplyDelete
  3. I totally agree with Dr. Nic. RSpec is ridiculously powerful because Ruby is powerful. Can you imagine trying to do RSpec (or even BDD in Test::Unit) with PHP or Java? Yeah, it'd work, but the pain threshold would be too high.

    And as for developers who "don't have time" to write tests: I sure hope they come around sooner rather than later. Because who has time to waste a weekend crawling through your app to fix the same bug as the previous weekend? Never again :-)

    ReplyDelete
  4. I agree. It isn't true that TDD makes Ruby a viable option. It is just that a good test suite is far more powerful to verify the correctness of an application than compilers are (which just verify the syntax).

    ReplyDelete
  5. Anonymous7:49 AM

    I totally agree with the last paragraph. We went a good couple of years developing "professionally" with Rails without a single test.

    Since we have been testing thoroughly (around a year now), the main difference is that we have far less phone calls with small bugs as instead of the client informing us, our test suite does.

    How did we go so long without tests? I honestly don't know how we survived without testing.

    ReplyDelete
  6. Rails has brought a lot of people to Ruby. Just before seeing this post, I was reading the testing section of Obie Fernandez's excellent book "The Rails Way" - in the intro to that chapter he points out that "David and everyone in the Rails core team are true believers in automated testing and they lead the community by example. [...] From the beginning, testing has been an integral and essential part of the Rails way which sets Rails apart from the majority of other frameworks out there."

    I have to say that, as a web developer for over ten years, Rails is what really proved TDD for me, and I have seen the same effect had on many of my colleagues.

    Rails is as responsible for the growing popularity of TDD as anything I can think of.

    ReplyDelete
  7. Anonymous2:38 PM

    @keith - CITCON seconded! Come along Jay!

    ReplyDelete
  8. Anonymous10:59 AM

    I know this post is old, but in addition to Jay's statement "TDD is not mainstream", I'd say "What many would agree to be 'True TDD' is so rare to be almost non-existant". I've worked at a few places now that have claimed to do TDD but didn't. Including myself there are many missing tests, tests written after the fact, or no tests at all. Teams and managers basically lie about TDD because it is a cool buzzword. The fact is that testing is an art just like (non-test) development, and TDD and BDD are both ideals that very few can reach. The developers that know how to write tests correctly is so rare, and only those developers can really do TDD, because the rest can end up with a nest of poorly written brittle tests that don't fully test the code. I think people would be better off just admitting that TDD and BDD are ideals and saying they are "trying" to do them rather than doing them. So, in my opinion, the answer to the questions "Did TDD make Ruby a viable option?" or even on its head "Did Ruby make TDD a viable option?" are both "No."

    ReplyDelete
  9. Anonymous11:03 AM

    BTW- if I'd had tests, maybe my last comment would have been more legible! ;)

    ReplyDelete

Note: Only a member of this blog may post a comment.