Live data from Hacker News

Ask HN: Do you write tests before the implementation?

news.ycombinator.com

121–130 of 329 posts

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

#121

Yeah. I also floss every day, clean up the kitchen as I cook, keep off-site backups of my personal data, call my mother regularly to thank her for raising me, read the terms and conditions to online services, keep an up-to-date to-do list, and change all my passwords once a month.

I believed it until your password remark. That's a bad, bad practice.

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

#123
post #110

Earlier quoted context omitted.

I would agree that "coverage gives an idea of how many lines of code have been run, but obviously no guarantees of correctness for those specific lines"

Perhaps I should rephrase. If you're trying to effectively unit test a project, when do you decide you've tested enough? And are there any metrics which help support that? Coverage for example is a weak signal that you've at least run some fraction of your codebase at test time.

I just write tests for the bits that I know I'm going to have lots of trouble with. You can tell, after a while

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

#124
I think there's this myth that TDD is one of the best ways to write software and if you admit you don't do it, you'll be seen as a cowboy and will look stupid. I think the truth is TDD has its pros and cons, and the weight of each pro and con is highly dependent on the project you're doing.

- The uncomfortable truth for some is that not doing any testing at all can be a perfectly fine trade off and there's plenty of successful projects that do this.

- Sometimes the statically checked assertions from a strongly typed language are enough.

- Sometimes just integration tests are enough and unit tests aren't likely to catch many bugs.

- For others, going all the way to formal verification makes sense. This has several orders of magnitude higher correctness guarantees along with enormous time costs compared to TDD.

For example, the Linux kernel doesn't use exhaustive unit tests (as far as I know) let alone TDD, and the seL4 kernel has been formally verified, both having been successful in doing what they set out to do.

I notice nobody ever gets looked down on for not going the formal verification route - people need to acknowledge that automated testing takes time and that time could be spent on something else, so you have to weigh up the benefits. Exhaustive tests aren't free especially when you know for your specific project you're unlikely to reap much in the way of benefits long term and you have limited resources.

For example, you're probably (depending on the project) not going to benefit from exhaustive tests for an MVP when you're a solo developer, can keep most of the codebase in your head, the impact of live bugs isn't high, the chance of you building on the code later isn't high and you're likely to drastically change the architecture later.

Are there any statistics on how many developers use TDD? There's a lot of "no" answers in this thread but obviously that's anecdotal.

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

#125

Earlier quoted context omitted.

To each their own. I have seen many times all the unit tests pass, but the application is broken. It really depends on the situation. There is no silver bullet when it comes to testing.

Yeah that's why "every" open source project you trust has a shitload of unit test. For amateurs, no tests, pros write test always.

[deleted]

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

#126
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 tool. I want my tests to deliver high enough value to warrant their ongoing maintenance and costs. That means I don't write nearly as many tests as I used to (in my own projects), and far fewer than my peers.

Where I work, tests are practically mandated for everything, and a full CI run takes hours, even when distributed across 20 machines. Anecdotally, I've worked for companies that test super heavily, and I've worked for companies that had no automated tests at all. (They tested manually before releases.) The ratio of production issues across all of my jobs is roughly flat.

This issue tends to trigger people. It's like religion or global warming or any other hot-button issue. It would be interesting to try to come up with some statistical analysis of the costs / benefit of automated tests.

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

#127
I don't do TDD on the first version. For me, the first version is a throwaway version. If it turns out to be commercially viable, that's when I start with a brand new codebase incorporating all the lessons from the first version, but this time using TDD. TDD has it's place. I just don't think it's cost effective on the first version.

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

#128
post #15

Never done this, and don't consider it practical. Code and interfaces (even internal ones) change rapidly for me when I'm starting a new project or adding new major functionality to the point that the tests I'd write at the beginning would become useless pretty quickly. I also believe that 100% test coverage (or numbers close to that) just isn't a useful goal, and is counterproductive from a maintenance perspective:…

What "other kinds of testing" do you do instead then? How do you make sure the code is testable by those other tests?

Often people fall back on manual testing, which is often slow, unreliable and incomplete. And certain things might not even be testable if the system hasn't been designed to allow it.

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

#129
No, because even if I write the test before the code, the real test will usually be written after the real code. That is to say, I write a test, then I write code, then not much later I change the code to different code, and then I have to change the test to a different test anyways.

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

#130

I can't even code like that except maybe for simple tasks in a mature project. I'm more a "Make It Work, Make It Beautiful, Make It Fast" person and don't see it working by writing unit test first.

I think the value of TDD's red–green–refactor cycle is in making sure that after you "Make It Beautiful" it still works, and again after you "Make It Fast" it still works. Otherwise, if you don't automate the test first, you end up testing manually three times.
Post reply on HN