News for hackers. Stuff that matters.
Re:Pledging for unit tests by tomjen3 on Friday October 29, 2010 @06:28AM (#1846792)
Unlike, it seems, everybody else I find unit tests to have very little use, but hey if you want them do so.

TDD on the other hand is both an unprofessional waste of time (write code that you know isn't going to work just to make a test that you know isn't comprehensive enough pass? Really?), and an unprofessional way to structure your design - instead of a bunch of well behaved classes that abstracts and neatly encapsulate their behavior, you have to insert hooks so that you can inject your mocks.

At the end of the day, you end up testing software and find some, but far from all, of your bugs.

If you are concerned about software quality, great - write better code, do root cause analysis, but most of all: use a language with a good static type system.

Re:Pledging for unit tests by DeusExMachina on Friday October 29, 2010 @07:34AM (#1846908)
I really agree with this. The main guideline I keep in mind when I write code is the old adage from Abelson & Sussman: "Programs must be written for people to read, and only incidentally for machines to execute". Readability is the main reason I get code that is very maintainable and with very few bugs. For those that still get into the code (they always do), it's very easy to find them thanks to the good structure and readability of the code.

Unit testable code becomes ugly and unreadable very fast. The main goal becomes to make it testable instead of making it readable. It pained me every time I had to do this in the past, and I did it because some company wanted it no matter what. I don't do this in my personal projects and they are almost bug free anyway (no unit testing does not mean no testing at all).

In TDD you spend time making your code ugly and you probably introduce bugs in doing this, and then you spend other time to find and fix those bugs. And when you need to go through that code again for fixing or refactoring it's harder to understand because of all those unit testing hooks.

For very big systems or for applications with a certain structure it probably makes sense to have unit tests. In functional languages they are a lot better because you usually don't have to change the code to adapt to it. But for a lot of applications they make no sense at all.

So please, stop selling them like a panacea for everything. Even better, please stop selling anything like it is a magic medicine for every illness.

Re:Pledging for unit tests by gurraman on Friday October 29, 2010 @06:49AM (#1846828)
If you see no added value in unit tests you're already doing something to ensure that your programs run as expected when you refactor, try on different platforms, try different input data etc. It would be interesting to hear more about how you test your software. Either we can learn from you, or you can learn from us (we/us being people who use formalized tests).

Cheers!

Re:Pledging for unit tests by mononcqc on Friday October 29, 2010 @11:42AM (#1847659)
I'm not sure any type system can even notice that I returned True instead of False, had a 'not' where I shouldn't, that I forgot about operator precedence in some spot, that the string I return indeed has the format asked by in a specification, that I validated all input correctly, etc.

Unit tests are useful for all these ugly things that come from developers not paying attention to something. I've found them extremely useful as a sanity check when I get a particular specification (might it be a design document, an API or a specific RFC).

Then again, the testing I usually found the most useful had to be integration testing. I would just sit with the testers, see what they expect the program to be and basically translate the test cases they would do (or that I would do in a REPL) into a module and run it. Lots of time saved for everyone in the process.

Re:Pledging for unit tests by dstorrs on Friday October 29, 2010 @12:58PM (#1847943)
I think there's a "baby and bathwater" issue here, on both sides.

IMO, the basic principle of TDD is solid: write the tests, then write the code. Like most ideologies, this takes an acorn of wisdom and grows it into an oak tree of folly (or some other really pompous metaphor -- that sentence rather got away from me).

The problem comes when people think the tests are something valuable in and of themselves. They aren't. They are only valuable insofar as they show a developer (a) if something is broken and (b) what it is.

I generally agree that Mocks are more trouble than they are worth -- they are an entire mini-codebase that doesn't contribute business value and must be maintained. But usually you can get most of the benefit without spending the effort: e.g. instead of a Mock::DB, have a separate instance of your prod DB running a snapshot; instead of a Mock::HTTPConection, just do a real HTTP request.

In short, I relate to your philosophy and share it to a certain extent, but I encourage you not to let the TDD zealots drive you away from what can be a useful tool.

Re:Pledging for unit tests by tomjen3 on Friday October 29, 2010 @04:29PM (#1848892)
>IMO, the basic principle of TDD is solid: write the tests, then write the code.

This is where we disagree: the value of the code is secondary to the tests (this shouldn't be that big a point of disagreement: would you rather have no tests and the code of a functional program, or great tests and no program?), but if you design the tests first, you are going to design the program such that it is easy to write tests for.

Write the program first, then write tests for it in the places where it is reasonably worth your time to do so.

Re:Pledging for unit tests by dstorrs on Saturday October 30, 2010 @02:04AM (#1850240)
Ok. I'm not actually that wedded to the order you do the two operations in. I think there is some value in writing tests first, so that you can verify they fail (before the code is written) and then work (once it is complete). But I think it's less important to do them first than to do them.

IMO, the "killer app" for unit tests is regression testing. If I make a change and the test suite still passes, I can be reasonably sure that I didn't break anything. All the other benefits are "nice to haves" in my mind.

Re:Pledging for unit tests by yock on Friday October 29, 2010 @07:45AM (#1846931)
Perhaps you wouldn't mind sharing your own personal testing methodology then?