There is some controversy among developers whether TDD (Test Driven Development) is really worth all the extra time it seems to take.
To answer this, first we must define what TDD is! Basically, it means developing a small piece of functionality by first writing a test for it, then code to make that test pass. For instance, in making a job board, a piece of functionality might be "get a list of jobs with given text in the title". So, you might write a few tests like "with an empty database, create a job with the title 'Java Developer', ask the Job class for all jobs with 'Java' in the title, and assert that I found that job", and the same but looking for 'Ruby' and assert that you didn't find it.
(This is not to be confused with BDD, or Behavior Driven Development. BDD is like TDD from a user's point of view, rather than a developer's. It usually uses much more English-like language, so as to let non-technical stakeholders be involved. This can help narrow the gap of understanding between them and the developers. Many people do BDD for the broad overview and then TDD for the nitty-gritty internal details.)
Most developers, however, go a bit further. To most, TDD is a cycle of "red, green, refactor":
- Red: write a test, to test whether the code (that you haven't
written yet) does what you want... and verify that it
fails. If it doesn't, then your test is meaningless!
(Writing good tests is an art unto itself, which I won't go into in this
- Green: make the test pass... and keep the whole test
suite passing! If your code broke anything else, and you must now
go fix the breakage, whether that means updating an outdated test, tweaking
your new code, tweaking old code, etc. You can't call it "green" until
the whole test suite passes!
- Refactor: this is what makes it "go further". To refactor a
piece of code means to improve the internal design, without altering the
behavior. There have been many books written about this, so I
won't go into detail; just know that, even above and beyond the benefits of
the red and green parts, TDD practitioners feel a responsibility to clean
it up. If the way you got the test to pass was a horrible little
kluge (admit it, we've all done it!), make it right before you check
One of the dirty little secrets of TDD is that, yes, it will slow you down... in the very short term. If you just want to get a feature implemented today, and don't care about tomorrow, you might be better off skipping testing, whether before or after coding.
That would not be wise in the long run, or even the medium run. You have to think of it as an investment. (This pairs quite nicely with the notion of "technical debt".)
TDD will help you get that feature to market even more quickly than skipping the tests, and with far better quality! The process of getting a feature not just implemented but also to market allows enough time for bugs to be noticed, and have to be fixed. And for other features to be added, that might interfere with this one. And bugs to be noticed in that other feature, whose fixes might interfere. And situations to crop up that you just didn't anticipate.
THAT is where TDD will save your bacon! The test suite, that you grow along the way, will help get those features implemented, and bugs fixed, without breaking other features. It's almost like giving you guard rails. If something does break, then the test suite will help pinpoint it, saving you hours of exploratory manual testing. In the long term it will save the project hours of exploration, debugging, finger-pointing, and other such nonsense... and if you're really doing Test-DRIVEN Development, probably guide you to higher quality code in the first place, saving months of disentangling and re-implementation.
But how does TDD do that? First we have to define what we mean by "quality". The two main things TDD will help with are, from a general standpoint, "it does what it's supposed to do" (including not having bugs), and from a geekier standpoint, "it has better internal design".
The first part is obvious. After all, that's what the tests prove. But what about "better internal design"? What does that even mean? There are many aspects of software design, but TDD guides you to think in terms of small easily testable pieces. This leads to code that is more reliable, modular, reusable, flexible overall, and a host of other benefits. For this reason, some people are now claiming that TDD should stand for Test Driven Design rather than Development. Perhaps our more DoD-minded colleagues will call it Test Driven Design and Development, or TDDD, or T3D for short, in much the same way they keep coming up with more C's to precede an I. ;-)
Of course, if you include the Refactor part of the cycle, that's another investment, one that usually pays off quite well in the long run. Paying attention to proper design early will make it much more likely that the code will stand the test of time, lasting much longer before needing to be totally chucked out and rewritten. We've all seen code so horrendous that we'd rather start over from scratch, rather than modify it -- don't be "that coder".
TL;DR: TDD does make it take longer to implement a feature... but not to get it
to market, and it yields much better code, saving even more time and expense later.