Live data from Hacker News

Ask HN: Do you write tests before the implementation?

news.ycombinator.com

151–160 of 329 posts

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

#152

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...

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 to keep yourself busy while listening to an interesting podcast.

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

#153
I do sometimes. It depends. I want to do more of it.

Here are cases where I've genuinely found it valuable and enjoyable to write tests ahead of time:

Some things are difficult to test. I've had things that involve a ton of setup, or a configuration with an external system. With tests you can automate that setup and run through a scenario. You can mock external systems. This gives you a way of setting up a scaffold into which your implementation will fall.

Things that involve time are also great for setting up test cases. Imagine some functionality where you do something, and need 3 weeks to pass before something else happens. Testing that by hand is effectively impossible. With test tools, you can fake the passing of time and have confirmation that your code is working well.

Think about when you are writing some functionality that requires some involved logic, and UIs. It makes sense to implement the logic first. But how do you even invoke it without a UI? Write a test case! You can debug it through test runs without needing to invest time in writing a UI.

Bugs! Something esoteric breaks. I often write a test case named test_this_and_that__jira2987 where 2987 is the ticket number where the issue came up. I write up a test case replicating the bug in with only essential conditions. Fixing it is a lot more enjoyable than trying to walk through the replication script by hand. Additionally, it results in a good regression test that makes sure my team does not reintroduce the bug again.

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

#154

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…

very good point.

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

#155

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 imagine you do some of these things, some of the time.

Do you write some tests, sometimes before coding something?

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

#156

In new code, I'll usually write high level black box tests once enough code is in place to start doing something useful. I rarely write unit tests except for behavior that is prone to be badly implemented/refactored, or for stuff that's pretty well isolated and that I know I won't touch for a while. Then as the project evolves, I start adding more high level tests to avoid regressions. I prefer high level testing of…

Unit tests actually are the most important ones... I do not wanna work on any of your projects heh.

If I have time to write only one of unit or functional test, I will always choose a functional test, because these ones test what the user actually sees.

It's possible to write an app that passes 100% of unit tests and still be completely broken to the user.

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

#157

Earlier quoted context omitted.

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

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.

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

#158

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.

Enlighten me, why?

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

#160

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 do it too. But incrementally.
Post reply on HN