News for hackers. Stuff that matters.
Re:Good code needs few unit tests by dusklight on Friday March 19, 2010 @05:18PM (#1205309)
This argument is not sound at all. The statefulness of your code has nothing to do with why we write unit tests. Even if you write a purely stateless function, it would still be useful to write a unit test to check for correctness. If you are doing TDD, you wrote the test before you wrote the function and that way you know exactly when you are done (because the test now passes). If you wrote your test afterwards, it is still useful if you ever change the behavior of the function, because any unit tests that were written that use this function will now let you know where and in what manner your change has affected the entire code base.

If you are writing bad stateful code that has a lot of bugs and you are not smart enough to figure out how all the states interact, unit tests will help you a lot, but there are many other scenarios where unit testing is useful.

I would not say good code has few unit tests. I say arrogant code has few unit tests. If you write few unit tests, as the size of the codebase grows, you have increasingly less certainty about how any change to the existing codebase will affect other parts. When the codebase is small, this is not a problem. If you are very intelligent, the size of the codebase that you can manage in your head might be quite large. But you are just wasting mental capacity that might have been doing so many more interesting things if you didn't have to remember everything and could trust your unit tests to give you the information you need when you need it.

Re:Good code needs few unit tests by jhancock on Friday March 19, 2010 @05:34PM (#1205344)
I'm not completely against TDD. However, one thing I've found is bugs are caused by things you didn't think to write a test for (or write your code to handle sans tests). The mundane stuff I see in the vast majority of unit tests are things an experienced programmer almost never screws up. So how do you write a test for a case you couldn't foresee?

My biggest argument in favor of writing tests is to ensure my code doesn't break relative to other people's code. There are many times I've encountered an update to a ruby gem with no major or minor version change that has broken a, usually implied, contract with my code. I say implied, because duck typers, at least what I've seen in the ruby world, tend to be slack about formalizing interfaces. I write tests to protect against that sort of thing.

Re:Good code needs few unit tests by youngian on Friday March 19, 2010 @05:39PM (#1205355)
So how do you write a test for a case you couldn't foresee?

You don't, but once you discover it, you write a regression test to make sure it doesn't come back.

However, IANATDD, I am not a test-driven developer - I don't see any reason to be dogmatic, but I do think unit tests can be extremely valuable. So this answer might not be TDD-approved.

Re:Good code needs few unit tests by InclinedPlane on Friday March 19, 2010 @07:42PM (#1205582)
"So how do you write a test for a case you couldn't foresee?"

"You don't, but once you discover it, you write a regression test to make sure it doesn't come back."

Sure, that's SOP in any good dev. house that does testing. But you've failed to address a key weakness in TDD. The real question is how do you discover these sorts of defects given that a single developer writing in a TDD style is unlikely to do so? TDD is clearly not a cure all, and this is a major weakness of it. Other development techniques, many of which can complement TDD, such as formal code reviews and beta testing can do a better job of getting you to higher product quality than TDD alone.

Re:Good code needs few unit tests by chuckm on Friday March 19, 2010 @11:26PM (#1205907)
"TDD is clearly not a cure all"

Who said it was?

Re:Good code needs few unit tests by binspace on Saturday March 20, 2010 @12:50AM (#1206000)
Exactly!! There is no such thing as a cure all. Anybody angry over a technique that does not solve all problems is clearly suffering from the Silver Bullet Syndrome.
Re:Good code needs few unit tests by devinj on Saturday March 20, 2010 @02:17AM (#1206090)
What? Why does something have to be perfect for somebody to get angry over it?
Re:Good code needs few unit tests by binspace on Sunday March 28, 2010 @05:57PM (#1225317)
Not exactly sure what you are asking. I mainly meant any tool you use will not solve all of your problems. Pretty basic common sense, IMO.
Re:Good code needs few unit tests by grandpa on Friday March 19, 2010 @10:15PM (#1205813)
> So how do you write a test for a case you couldn't foresee?

TDD is a design technique, not a testing technique.

Re:Good code needs few unit tests by erso on Friday March 19, 2010 @05:55PM (#1205383)
For me, I don't always know what the edge cases could be until I write them down. Writing tests allows me to get a clearer picture of what kind of coverage I have.
Re:Good code needs few unit tests by binspace on Friday March 19, 2010 @06:05PM (#1205404)
Another nice thing is you can read what the edge cases are going to be, rather than having to remember them all of the time.
Re:Good code needs few unit tests by binspace on Friday March 19, 2010 @05:54PM (#1205380)
TDD does not claim to fix all of your bugs. You often do not need to theoretically prove your software is correct, so you don't need to have you unit tests test all possible contexts (e.g. handling disk failure vs. any other error when rendering a web page). That is what abstraction is for.

TDD does force you to think about the interface of your abstraction, however. If it is painful to write (or maintain) the test, then that is a good indication that the abstraction is wrong or not being properly tested. Don't be afraid of deleting tests when they are not useful.

I often find that these unanticipated bugs are often due to incorrect abstractions. To fix it, create the correct abstraction and test it.

It is also simply faster to develop in larger projects than creating the state that would test your unit. For example, you could make a change, manually refresh the page, and debug or write the test and make it pass. You also get the benefit of regression testing.

Note that the narrower your unit, the less likely it will be useful for regression purposes.

I often find that when beginning a project, my units are smaller. As abstractions are built, the nonessential tests are deleted and future tests are built upon abstractions that I care about.

Re:Good code needs few unit tests by moss on Friday March 19, 2010 @09:13PM (#1205744)
I think I'm blithely misreading the article here, but I'm inclined to take it as advice about how to do TDD, rather than an argument against it. Specifically: if a class has lots of tests, this is a sign that the class is too complicated. Refactor until it is simple enough not to require as many tests (possibly by narrowing its interface, possibly by breaking it up into multiple classes, possibly by rethinking how it's implemented).
Re:Good code needs few unit tests by allyt on Friday March 19, 2010 @10:03PM (#1205798)
From what I know, Andrey (the poster) is pretty neutral on TD, maybe even a proponent. He's saying you should have an interface in mind when you start to write unit tests. Sometimes you can do that before writing code. Other times, exploring with a REPL first is better.
Re:Good code needs few unit tests by eru on Friday March 19, 2010 @08:08PM (#1205643)
> Even if you write a purely stateless function, it would still be useful to write a unit test to check for correctness.

Indeed. Tests are a good idea in, say, Haskell, too.