18 points · by koski · 16y ago · 48 comments
Granted, every project can benefit greatly from unit tests. As for TDD, it is an admirable practice, but let's not get religious.
Does TDD deliver what the OP says it does? I think so (anecdotal evidence, so, worthless), and there is research out there to support it [1] (possibly valid evidence). It seems we have at least a basis for debate.
Therefore, if you don't use TDD, you should raise objections to the existing research you have. Or, offer some anecdotal evidence in opposition to TDD. Then perhaps we could conduct experiments to test the corresponding hypotheses and finally converge on a verdict regarding this. I think if we (as a field) fail to do that, we are unprofessional.
[1] Evaluating the Efficacy of Test-Driven Development [pdf]. http://research.microsoft.com/en-us/projects/esm/fp17288-bha...
Not too surprising, most methodology studies that I've seen can't exactly claim scientific rigor. (Yes, it's hard to do when it comes to real-world projects, but other fields of scientific study manage better.)
If you meant that they weren't rigorous in controlling variables related to team composition, I'd agree. However, I think those variables can safely be ignored if the study is repeated and validated many more times (which the authors acknowledge is a work in progress).
Very good reason? Yes, it's very costly and only catches a certain sort of error.
Would clients prefer 25% more features or the set you have with a full suite of unit tests? Many would prefer the 25% more features.
Additionally, with that same amount of money you could spend doing TDD, you can do any number of OTHER practices which ALSO increase code quality, including, formal design reviews, informal design reviews, informal code reviews, formal code reviews, desk checking of code, automated analyzers, running through profilers, running through memory leak detectors, modeling, prototyping, component testing, integration testing, regression testing, system testing, beta testing and many more techniques.
All of these take TIME and MONEY. TDD is just one of the many things you can do if your client/employer cares about code quality, and not necessarily the best thing. I mean formal inspections (according to code complete 2, pg 470) and formal design reviews are always much more efficacious at finding bugs than unit testing (which is what TDD revolves around). According to that, we shouldn't be spending the quality improvement time doing TDD based procedures then, but instead pouring over the code in review sessions.
Secondly, many other techniques CAN yield better results than TDD, and in less time (but don't always). These include protoyping, personal desk checking, system tests, and beta tests with more than 1000 users.
So many adherents of methodology X don't accurately scope in the tradeoffs of their costs, etc, when advocating their system. Morning scrumm meetings sacrifice people who want to have long (and therefore, more unpredictable commutes). People who like automated function comment headers don't count how much time people spend filling in those header boxes. People who like variable naming conventions don't spend enough time understanding what that does to the people who have to constantly change the way they name them. People who like full waterfall don't understand the morale crushing cost of huge changes after large design/documentation pushes like that. I could go on and on.
TDD has costs. Those costs aren't necessarily worth it. Sometimes they are, sometimes they're not. The people who say "it saves you time" are full of crap. It may, sometimes, save some people, some time. All the tests, if they're at all worthwhile, both take time to write and maintain. I've been on projects which use it and don't, and while there was a lower defect rate on certain types of bugs, those weren't always very important, nor was the time cost of writing the tests worth it, as those projects had several high value features which couldn't be completed due to time.
TDD makes pretty specific claims. Most of those are testable (at least to some extent). To assert that it's all relative is to dismiss the possibility that TDD advocates are onto something. As I outlined before, outright dismissal is arrogant and unprofessional. Please, back your assertions with evidence so they can be debated.
Edit: Removed an paragraph that diluted the point.
Maybe a good follow up question would have been "...or any other kind of automated testing?"
If there are not any kind of automated tests, I think there is a good chance the project is in trouble, or will be shortly.
There's a big difference between telling someone, "the code you're delivering is not meeting objectively-decided maintainability goals for this project", and micromanaging the personal workflow which someone uses to acheive these goals (test first, test soon after, use TDD as your primary deisgn methodology vs use whatever design methodology has worked for you in the past).
Trying to do the latter, especially when it's done with a tone of superiority and "you're doing it wrong", I think sums up why TDD advocates often tend to rub people up the wrong way -- especially experienced programmers.
The cost for making software that you know works, for the formal definition of "know"[1] is very, very high. That level of quality is usually reserved for space shuttles and similar.
TDD makes software as good as the tests you write. No more, no less. It can be a great aid in producing better code, and better tested code, but in the end all software development is a trade-off between time spent and quality. You have to decide where the line for "good enough" is. TDD itself doesn't magically move this line to "perfect" without incurring a corresponding cost in development time.
[1] A knows B iff B is true and A believes B and A has good reason to believe B.
First of all, 40% is way too high. Wonder what kinds of conferences they attend…
Also, even if you buy in totally to the agile premises, knowing whether your code works should be an effect of code coverage by unit tests, whether they're up-front or not isn't an issue at all.
And finally: No, we're not "professional". This is Hacker News.
In my opinion, the most important thing for code is for the developers to actually care about it. Whether you test first, test later, or just simplify the code until it's obviously correct [1] it really doesn't matter, just care enough to want bug free software.
[1] "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." (Tony Hoare)
"Beware of bugs in the above code; I have only proved it correct, not tried it."
- Don Knuth
What?! No. No, it doesn't mean that.
- True TDD requires 100% test coverage of the code the developer using TDD has written or altered. I've seen the reports from companies or organizations claiming they do TDD that aim at least 60%. That isn't true TDD, in the Kent Beck definition of the term. That's not to say these people are wrong. In fact, because they have a better balance between test code and production code, they can change functionality of code more easily.
- Many developers attempting to practice TDD don't write the tests well enough to cover every possibly argument/configuration for the various methods they were testing. That's not TDD.
- Many developers attempting to practice TDD won't test trivial methods like getters and setters. That's not TDD.
- Many developers attempting to practice TDD write tests that duplicate the parts of the code being tested. While there are valid reasons for this, it results in frustration for the developer who needs to change 20 tests just to change one bit of functionality. Avoiding this in many cases can take an enormous amount of discipline and time, ensuring proper modularization, mocking, and refactorization of both production code and tests.
So, why do people claim to be doing TDD that aren't? Because there is not a good term out there that everyone knows of for just writing tests before writing code some of the time, duplicating functionality of tests, and leaving out functionality tested by many tests.
In some way, writing and maintaining tests is a lot like auto maintenance. TDD is like keeping your car in pristine condition. It is super-shiny because it is always just-washed and sparkly, and it runs like a champ. But in the end, the primary reason for the car is to get you from point A to point B.
That's ridiculous and impractical. What if my function fails for values of t >= 946684800?
I'm not promoting real TDD, btw. Real TDD is fine as a ideal, and is achievable in many circumstances and even may make perfect sense even in an ongoing basis in some environments, but it isn't practiced to the percentage overall that the OP (in the linked post) stated, and developers that heartily promote TDD usually don't fully understand its implications when practiced fully, or what doing true TDD really involves. I'm not against writing tests, but at some point, you need to relax.
- "Timeline ticks don't line up with grid when changing scale"
- "Resizing shapes is jerky in FireFox"
- "Need a button you can hit to pull up a simple Help screen"
- "Add an opacity slider for shapes"
Unit testing rendering correctness during mouse actions? Unit testing for UI features? Unit testing for CSS? Hacka please.
If you're doing little comp-sci type things on the server then sure you can do TDD. It makes sense in the context of building a string library or wrapping somebody else's API. In the web startup world though, how often are you actually doing that stuff?
Are you implying that your web site does not have any "little comp-sci type things on the server" aspects?
"Add an opacity slider for shapes"
This seems like the kind of thing that probably does have a little comp-sci part to it. Do you have an abstraction for shapes? Maybe a unit test for the API underlying the slider, that validates that all selected shapes now have the opacity value passed from the slider?
Maybe that doesn't fit the design of your code, but I strongly suspect there is some unit testable part of that task. The point being, unit testable things should have unit tests, and most tasks might have some unit testable part.
For the issues I listed above, how many of them do you actually consider to be testable, using TDD's "write your tests first" definition of the word.
Doing TDD - that is, writing tests up-front - forces you to not only consider the correctness criteria for your code (and gives you the confidence that the code, once written, works correctly), but it also forces you to design your code for test, which enforces separation between components (so that they're individually testable), generation of sane interfaces (so that simple mock objects can be written) and so forth. It's not just about making code that you can be confident in, it's about making saner code for the long term, too.
http://ravimohan.blogspot.com/2007/04/learning-from-sudoku-s...
It seems that the blog post got attention before it was correctly reviewed. Damn you guys are fast :-)
The idea was not to push TDD or promote it in any way. It was a rant to devs who don't do unit tests. Someone else needs to one day fix their untested code without knowing if changing something in X will break Z and Y.
Even the title of the post is changed now. Seems to be too late though.
By the way, your comments are great, they gave me many thoughts.
For instance, take the particular long, messy function that you happen to be working with right now, then write a bunch of tests for that function. Test that invariants are maintained, bad arguments or pre-conditions are handled, post-conditions are honoured and so on. You don't even have to know whether what you are testing is correct behaviour at this point. Just get as much test coverage as you practically are able to.
Then you can start TDDing smaller functions that are made up of bits and pieces from the big, ugly beast-function. You can develop and test these in complete isolation from the running code.
Finally you start replacing bits of the ugly beast with calls to your new clean functions, ensuring that you don't break any of those tests you wrote at the start. Over a period of time, you will gradually build up a set of unit tests for the most important parts of the system and you will hopefully gain enough confidence to refactor it into something more maintainable. At least, that's the theory, anyway. :)
I would also strongly recommend reading Michael Feathers' excellent book, Working Effectively With Legacy Code (ISBN: 978-0-13-117705-5). It is the bible for this kind of work.
A book called "Working effectively with legacy code" by Feathers is great for instrumenting and regression testing old code bases then changing them without breaking them.
Non-aff link http://www.amazon.com/Working-Effectively-Legacy-Michael-Fea...
Is one unit test doing anything? Yes. Absolutely. It's giving you 1 more unit test then you had to begin with.
Once you have a test, you can write another test. You can refactor a method (And write tests). You can then refactor the calling methods. And so on.
"It will be a mammoth task" is only a good reason not to do something when it's either discardable in the near future (And not "When we start on V2!"), or if it's the canonical 9-woman-1-baby situation. If partial improvements give value, do them.