Live data from Hacker News

Ask HN: Do you write tests before the implementation?

news.ycombinator.com

81–90 of 329 posts

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

#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 "strange" code to accommodate testing. The downside is that I'm forced to plan code growth with steps that take me from one testable partial-product to the next. A more serious downside, one I'm very aware of, is that not every project is amenable to this approach.

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

#82
> Do you write tests before the implementation? Absolutely, day in day out. New code and bugs fixing a like. It's the proof that I need to know that whatever code I am doing is an exact fit to the problem it's trying to solve.

> Can you describe the practical benefit? Testing first help me clarify my intentions, then implement a realisation of those intentions through code. Testable code has the side effect of being well modularized, free from hidden dependencies, and SOLID.

And it's also about making sure that whatever code you write, there's a justification for it and a proof that it works, could be seen more like a harness protecting you from writing things that you don't need, YAGNI.

> Do you happen to rewrite the tests completely while doing the implementation? I follow the classic TDD cycle, RED/GREEN/REFACTOR and I can not be any happier.

> When does this approach work for you and when did it fail you? The only exception to the above is exploratory code. I.e. the times where I don't know how to solve a given problem, I like to hack few things together and poke the application and see what happens due to what I have changed.

Having verified and learned more about how to solve that problem, I delete all my code and start afresh but this time TDD the problem/solution equipped with what I have learned from my exploratory cycle.

If you are in doubt or need further information to help you make your own decision about the matter, I can not recommend enough the classic TDD by Example from Kent Beck as a starting point.

For a more real-world view with an eye on the benefits of adopting TDD, have a look at Growing Object Oriented Software Guided by Tests, aka the Goose book.

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

#83

Earlier quoted context omitted.

> Committing the red test and the fix to the test in two commits makes the bug and its fix easy to review. I don't think this is compelling argument. Normally the test and the fix are looked at together and are already sufficiently separated in the code. It makes more sense to me to use a single commit so the fix can be described in a single commit message, keeping the history clean.

Keeping it separate allows ensuring the test works easily: just revert the fix keeping only the patch for the test and make sure the test fails. My workflow tend to be to keep them separate, and write a big description in the merge commit rather than the individual commits (those have a small description of the local change). There's tradeoffs to both approach though, and as you mention, it's easier to keep track of…

I totally understand why you would do that and it makes sense, but I personally like to keep them in one commit so that I won't get any problems with an automated git bisect later.

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

#84
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 "strange" ?

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

#85
I tend to write test cases that re-produce bugs first, then fix the bug. Other than that, I don't stick too hard to test driven development. I did for a while, but you start to get a sense of the sort of design pressure tests create and end up build more modular, testable code from the get go anyway.

> Can you describe the practical benefit?

For a test case that produces a bug, you might find the bug manually. Getting that manual process into a test case is often a chore, but in doing so you'll better understand how the system with the bug failed. Did it call collaborators wrong? Did something unexpected get returned? Etc. In those cases, I think the benefit really is a better understanding of the system.

> Do you happen to rewrite the tests completely while doing the implementation?

A TDD practicioner will probably tell you taht you're doing it wrong if you do this. You write the minimum viable test that fails first. It might be something simple like "does the function/method exist". You add to your tests just in time to make the change in the real code.

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

#86
sometimes I do TDD for modules that are simultaneously developed for both frontend and backend (nodeJS)

This way I can mostly stay in the frontend where the tooling is better and the results are more visual, but still be confident that I don't violate the proper abstraction that the backend requires.

Kinda like predefining the shared module contract between front- and backend and forcing myself not to forget about it when in the (frontend) flow

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

#87
post #58

I do, TDD gives me such a sense of confidence that now that I'm used to, it's hard not to use. > Can you describe the practical benefit? Confidence that the code I'm writing does what it's supposed to. With the added benefit that I can easily add more tests if I'm not confident about some behaviors of the feature or easily add a test when a bug shows up. > Do you happen to rewrite the tests completely while doing the…

> It works better on layered architectures, when you can easily just test the business logic independently of the framework/glue code. It has failed me for exploratory work, that's the one scenario where I just prefer to write code and manually test it, since I don't know what I want it to do...yet

Totally with you on this, when I am clueless about the what/how, I throw a bit of exploratory code and test it manually or semi-automated fashion. But once the learning has happened I will use the acquired knowledge to feed a proper TDD cycle to do it properly now that I know a little better.

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

#88
I’ve always been highly skeptical of this approach. Often what you’re doing is so clear cut that tests are entirely unneeded. In fact, outside of the most complicated cases, I don’t even use unit tests. I have black box testing that I use to check for regression. My biggest reasoning for this is that test code is effectively another code base to maintain, and as soon as you start changing something it’s legacy code to maintain.

All that being said, I haven’t spent much time on teams with a particularly large group of people working in one project. I think the most has been 4 in one service. The more people working in a code base, the more utility you get from TDD, I believe. It’s just tough to have a solid grasp on everything when it changes rapidly.

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

#90
This is a big topic, and you're asking some good questions. Rather than tackle it all, I can recommend expanding your questions with the following.

When someone tells you they don't write tests first, ask them how they refactor. How do they know the changes they made didn't break anything?

You can fool yourself with test-first, but it's quite difficult to do if you're rigorously following the practice. First write a failing test. Next, write only enough production code to fix the failing test. Optionally refactor. Rinse and repeat.

Code created this way can prove that every line of production code resulted from a failing test. Nothing is untested, by definition. The code may be incomplete due to cases not considered, but everything present has been tested. Note that it's possible to break this guarantee by writing production code unnecessary to get the test to pass.

Post reply on HN