Live data from Hacker News

Ask HN: Do you write tests before the implementation?

news.ycombinator.com

261–270 of 329 posts

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

#261
I found TDD unpractical. Can recall one situation when used TDD. I was writing a printf-like formatter in C++ and prepared a lot of tests cases in advance. That approach worked well at early state of development. however, further development had revealed quirks I hadn't predicted. As a result, the number and complexity of tests got increased.

My typical practice is to work out an API, write early scratches of implementation and test only simple cases. Then I can inspect two things: how the API works in real code and what else should be tested. In other words tests help to establish an API, then to stabilize the implementation.

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

#264
I realized recently that only 10% or so of the code I tend to write truly benefits from thorough testing. The rest can be handled more broadly through integration testing which is less about the code specifically and more about expected end use cases, like user workflows. I find those tests very useful. I only write those after the flow is established and more or less finalized.

I used to write a lot of tests and discovered over summer that it costs too much in terms of time spent writing, changing, and debugging tests for what you tend to get out of it.

I do think writing a lot of tests for a legacy or relatively old system is a great way to uncover existing bugs and document expected behaviours. With that done, refactoring or rebuilding is possible and you gain a great understanding of the software.

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

#265
I often list out and sometimes actually write many of the tests that’ll need to pass before I write much code. Definitely not real TDD, but just listing out the cases helps me stay focused, keep from missing things, pick back up after a distraction, etc. Before doing this I usually have gotten to the point where I've got classes and database migrations at least scaffolded out, but mostly empty.

I find literal TDD distracting and unhelpful, but having a list of things I need to handle that doubles as tests I can't forget to write is a really nice balance.

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

#266
I write tests NOT for correctness, but in order to refine the API for public methods/functions. If I'm going to have some fairly complicated behavior, I want to write an interface that is easy to use first, because otherwise I end up writing an interface that's easy to implement.

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

#267

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…

From what I understand about TDD it's more about getting the design right then testing for bugs.

TDD is about getting design right, but it's also about ensuring that the design's functionality is defined, demonstrated and protected from regression.

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

#268
I have never been able to "grok" the idea of writing tests before writing the implementation. My brain just doesn't work that way. It's like a speed bump that makes me lose the idea or inspiration if I try to think of it in terms of "tests" first.

However, when I need to overhaul something that already exists, e.g. the core of a game engine, I've gotten into the habit of writing tests for current behavior, so that when I rip it out its replacement works the same way, or at least retains the same interface, so I don't have to replace the whole pyramid on top before I can compile again. :)

This has also helped me realize the value of tests, but later on in the development cycle, not as the base before actually writing anything.

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

#269

Earlier quoted context omitted.

Can you give a run down on formal verification?

So say you were writing a sorting algorithm and with unit tests (perhaps with TDD) you wrote tests like: - sort([]) should produce [] - sort([1]) should produce [1] - sort([1,3,2]) should produce [1,2,3] - sort([1,5,6,2,3,4]) should produce [1,2,3,4,5,6] You would test a few values and edge cases until you were confident it works for all lists. However, you can't be 100% sure that there's some list out there like [5,…

> You would test a few values and edge cases until you were confident it works for all lists. However, you can't be 100% sure that there's some list out there like [5,5,5,5,1] that doesn't get sorted properly.

For the curious, something like this has happened before - and was found with formal verification: http://www.envisage-project.eu/proving-android-java-and-pyth...

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

#270
I've never had success doing this in languages like Java or Python, but where it's been very very helpful is in hardware description languages. Since you are often implementing a piece of hardware with known input and output specs, writing tests first can work and show you how much of you design meets the spec as you go forward.

Plus writing HDL without tests is basically guaranteed to create something nonfunctional.

I hate unit testing in for example Java though, individual functions are typically very basic and don't do much. A service? Integration tests? Sign me up, but unit testing to 100% coverage a function with ten lines that reads a bytestream to an object and sets some fields is boring, and fairly difficult to mock.

Post reply on HN