Live data from Hacker News

Ask HN: Do you write tests before the implementation?

news.ycombinator.com

211–220 of 329 posts

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

#213
I almost never write test beforehand, unless I'm working on a really complex subject, where I need to write all tests first to grasp the problem entirely. Otherwise, I write tests before submitting the pull request.

I almost never write tests for personal projects, but 100% of the time when working in a team. IMO, tests are not here to prevent bugs, but they are part of the developer documentation: a coworker must be able to make any change they want to my code without asking me anything, and tests are the biggest part of that.

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

#214

Earlier quoted context omitted.

It's a weird quirk of the brain how easy it is to wait until the food hardens onto the cooking utensils before cleaning it (sometimes needing a hammer and chisel to remove it) rather than just rinsing it off immediately with water. Yet, despite knowing this, picking the former route every damn time anyways. If you're like this, try putting on some small bluetooth headphones. Now cleaning the dishes just becomes a way…

Every time I clean right after I cook I'm stunned by how quick, easy and painless it is. You know, the two times a year I actually do it.

The difficulty for me is that a lot of food is best right off the stove. Then I have to clean it quickly afterward while happy and full, which is difficult.

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

#216
I look upto many persons in the industry who have achieved big milestones. One of them is Chris. His talk and post -https://quii.dev/The_Tests_Talk, will provide you with a tonne of information regarding TDD. If you are into GO, you should definitely check out Chris's book - https://github.com/quii/learn-go-with-tests.

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

#217

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 take the perspective that tests - and particularly unit tests - are a living specification for the software that you know cannot be out of date. If you write tests in a way that you understand the business purpose of the system, you are providing a significant part of the documentation while also providing a regression suite that is automated.

There are other ways to handle this. You can have a design document that is continually updated. Some teams can do this well. You can use literate programming a la Knuth. (Many code bases have abbreviated forms of this.) You can assume that the small number of developers who have been around for these decisions will never leave the company and forgo all of it. (I do not recommend this.)

So, what is your preferred alternative to unit tests as a specification? (And if your set of unit tests don't provide clarity to the source code, that may be a source of the frustration.)

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

#218
In my experience TDD is great when you know what you want a particular piece of code to do.

The other place it works well is code written as a pair - with one member writing tests and the other writing the implementation - the challenge is on to pass the buck back to the other pair member - i.e. find the obvious test that will cause the code to fail / find a simple implementation that will cause the tests to pass. This is great fun and leads to some high-quality code with lots of fast tests.

The benefit of TDD is that your coverage is pretty high - and you aren't writing anything unnecessary (YAGNI).

I don't think I have ever rewritten tests that I have written (TDD or otherwise). They might get refactored.

TDD doesn't work so well when you only vaguely understand what you are trying to do. This is not a coding / testing problem - get a better vision - prototype something perhaps - i.e. no tests and very deliberately throw it away.

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

#219
Like all things in life "it depends".

I rarely practiced TDD until I started working on a piece of software that could take anywhere from under a minute to an hour to finish running. This means I must be able to isolate the specific portion of code effectively to save time and focus on the problem at hand. The APIs for this model are well written so I can recreate a bug or test out new code effectively by stitching some APIs in a unit test. It's incredibly helpful in that sense.

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

#220

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.

Suddenly I feel very lonely for the fact I do actually clean the kitchen during and right after cooking...

I guess you own a house then!
Post reply on HN