Live data from Hacker News

Ask HN: Do you write tests before the implementation?

news.ycombinator.com

271–280 of 329 posts

Re: Ask HN: Do you write tests before the implementation?

#271
No at the start of a project. Yes for refactoring.

I used to write tests for all pure functions because they were the easiest tests to set up. They are also the easiest to debug so the test didn't really help unless you're checking for type signatures.

I think that implementation tests are important but I found that I suck at figuring our how to set up a test before the actual implementation. So I do them after the fact and judiciously.

Re: Ask HN: Do you write tests before the implementation?

#272

No. Tests are like any other code. They incur technical debt and bugs at the same rate as other code. They also introduce friction to the development process. As your test suite grows, your dev process often begins to slow down unless you apply additional work to grease the wheels, which is yet another often unmeasured cost of testing in this fashion. So, in short, I view tests as a super useful, but over-applied too…

I've honestly thought I was crazy for thinking that required tests are ridiculous. Thank you for confirming that I'm right in my beliefs.

At least 50% of my last job was writing tests, and the snail's pace of their dev process was the main reason I left.

Re: Ask HN: Do you write tests before the implementation?

#273
post #251

Nope. I pretty much always find it to be counterproductive. Most of programming happens in the exploration phase. That's the real problem solving. You're just trying things and seeing if some api gives you what you want or works as you might expect. You have no idea which functions to call or what classes to use, etc. If you write the tests before you do the exploration, you're saying you know what you're going to fi…

> You're just trying things and seeing if some api gives you what you want or works as you might expect. I don't do most of my programming this way, because mostly I'm writing new things, not gluing together existing APIs with a tiny amount of simple glue code. But when I do need to characterize existing APIs, I find that unit tests are a really helpful way to do it — especially in languages without REPLs, but even i…

You appear to be talking about unit tests in general, while GP was talking about test-driven-development (what the original question is about).

Re: Ask HN: Do you write tests before the implementation?

#274
post #67

Earlier quoted context omitted.

Do you consider coverage an effective metric? I've got some code that has a test suite which is effectively a bunch of low level driver checks, plus a bunch of common example snippets and checks for eg empty inputs etc. Coverage gives an idea of how many lines of code have been run, but obviously no guarantees of correctness for those specific lines (eg you can't detect a double negative). It's worked well for me so…

I definitely think coverage is a worthy metric to track. It can provide meaningful information about the "doneness" of your tests. It shouldn't drive testing though, and especially, you shouldn't write your tests specifically "to get coverage". Yes, lots of people do this in environments where "getting 100% coverage" is mandatory. That said, I've found issues specifically after targetting blocks for testing, which we…

> which were highlighted by incomplete coverage

This is the single most important part of code coverage IMO: we don't care about what the tests cover, we care about what the humans never considered.

For this reason I'm a proponent of 100% coverage with a major caveat: Any code you explicitly decided not to test gets marked "no cover", so it doesn't count for or against the coverage score. This way branches that were accidentally missed really stand out, and we're not bogged down by having to test 100% of the code.

Re: Ask HN: Do you write tests before the implementation?

#275
Never - I always attempt to make an end to end implementation. Once the code works I go back an examine the what I did and try to make it simpler. This often requires refactoring and changing API's etc. Writing a bunch of test would simply add inertia to that process. After I am satisfied with the code or get bug reports I will add test. Test code is code and often has bugs. Every line of code I write is a liability so I try to limit it to necessary things. I have never seen the write test approach work. Once all the test code is written people become reluctant to change things because they have to change both the code and test code doubling the work/time. One just ends up with well tested but not so good code. If you a M$ and can afford to pair program it might be more feasible.

Re: Ask HN: Do you write tests before the implementation?

#276

Earlier quoted context omitted.

Maybe you can help me understand this. Since you don't write as many tests, that means you're not actually testing all your code branches because tests incur technical debt after all. So does this mean you test every single branch manually? just don't bother with it at all? Do you just have a few integration tests and they break and you spend a good chunk of time figuring out which logical branch broke? What happens…

Yeah, as soon as code has more than two "real" branches, I don't trust myself to manually test them all. One of them will be broken quickly if I keep hacking in a particular branch. (This is, secretly, also an argument for writing code in sufficient generality to avoid this phenomenon in the first place.) I also never trust a test that passes the first time I run it. I am both terrible at writing correct code, and co…

I believe he's using the term branch different than you : 'alternate code path'

Re: Ask HN: Do you write tests before the implementation?

#277

No. Tests are like any other code. They incur technical debt and bugs at the same rate as other code. They also introduce friction to the development process. As your test suite grows, your dev process often begins to slow down unless you apply additional work to grease the wheels, which is yet another often unmeasured cost of testing in this fashion. So, in short, I view tests as a super useful, but over-applied too…

Maybe you can help me understand this. Since you don't write as many tests, that means you're not actually testing all your code branches because tests incur technical debt after all. So does this mean you test every single branch manually? just don't bother with it at all? Do you just have a few integration tests and they break and you spend a good chunk of time figuring out which logical branch broke? What happens…

> What happens if you make a typo, comment out a piece of code and forget to uncomment , etc?

Thanks god this is handled out of the box by any compiled language. I can't imagine myself writing tests to catch non-logical bugs.

Re: Ask HN: Do you write tests before the implementation?

#278

No. Tests are like any other code. They incur technical debt and bugs at the same rate as other code. They also introduce friction to the development process. As your test suite grows, your dev process often begins to slow down unless you apply additional work to grease the wheels, which is yet another often unmeasured cost of testing in this fashion. So, in short, I view tests as a super useful, but over-applied too…

Thankfully we don't have to. Hillel Wayne links to a few of the studies that have been done on TDD [0]. It doesn't have a conclusive effect on error rates in software. While I do write tests before I write code, more so in a dynamic language without a strong, static type system; it appears that there isn't any correlation to a reduced number of bugs. But I still do it. And I think that's because that while I may prev…

Don't you think that if tests are effective, they may let you go faster?

To put it another way, it is not surprising that error rates are similar since, presumably, you keep coding till you produce an error, fix it and then repeat.

The question is how much error free code you wrote between the errors. If testing -- or any technique for that matter, like relaxing, jogging, discussing, planning -- reduces errors, it will manifest as more functionality per error.

Re: Ask HN: Do you write tests before the implementation?

#279
post #81

Almost never. With the kind of software I mostly write these days, I'm fortunate to be able to incrementally develop my code and test it under real-world conditions or a subset thereof. So my approach is exploratory coding -- I start with minimum workable implementations, make sure they work as needed, and then add more functionality, with further testing at each step. The upside is that I don't have to write "strang…

> With the kind of software I mostly write these days, I'm fortunate to be able to incrementally develop my code and test it under real-world conditions or a subset thereof. What kind of software you write if you don't mind me asking ?and are your "real-world conditions" tests automated ? > The upside is that I don't have to write "strange" code to accommodate testing. Can you elaborate more as what you mean by "stra…

For the past 2 years, most of my work has been in porting some fairly simple legacy message forwarding and conversion programs from C to Java. So on our test servers I can swap out the C programs for drop-in replacements in Java and watch them (via log files) working -- or not. If my programs fail I can either observe crashes and stack trace or the message receiving programs will crash or loudly object to bad data from me. Usually one day's worth of traffic will exercise enough of my program's logic that failure to fail for a day constitutes a successful end-to-end test.

Yes, this is kid stuff. My current work is about as sophisticated as typical undergrad Computer Science projects. We can't all be doing rocket science!

I used to write automated test setups for my programs, providing streams of pre-canned messages and such. That worked out OK. I suppose it's great to have test suites to avoid regression and such, but I ended up regretting all the effort I sunk into testing. So far it's been my experience that I would sink a lot of time into creating a test suite that could exercise my programs as thoroughly as simple exposure to real-world message traffic.

I hope my attempt to be brief didn't come across as derogatory when I wrote "strange." Here's an example: I like to make a lot of my fields and methods private. It's handy that my IDE warns me when fields and methods aren't used, or when final fields aren't initialized. Obviously, for "classic" unit tests I'd have to at least expose my methods at the package level to call them from out of class. Another example: my apps rely on a fair bit of configuration data and some embarrassingly tight coupling between my classes. A JUnit-friendly program would call for a lot of mockups, as well as a lot more coding to interfaces rather than concrete classes, probably a lot more reliance on design patterns. My coding style for these projects yields a small number of compact classes but is very hostile to unit testing.

To be clear: For many other projects, your mileage may vary dramatically. I've successfully done TDD in other projects where that made a lot more sense.

Re: Ask HN: Do you write tests before the implementation?

#280
I know this will probably get downvoted. It's impossible to predict how your app will fail ahead of time. So test driven development (for the most part) is a waste of time. Every test you write will either continue to pass forever not providing useful information to you or will need to be updated when new features are added to the software, making them costly to have in place. Meanwhile they reveal very few defects that wouldn't easily be caught with a basic smoke test you need to do anyway.

Of course, there are always exceptions. if you have software that is highly complex but the outputs are very simple and easy to measure then it might actually be a good idea.

Post reply on HN