Live data from Hacker News

Ask HN: Do you write tests before the implementation?

news.ycombinator.com

111–120 of 329 posts

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

#111

mostly tests while doing implementation, but not 100%. however, I've started working on a project with others, and am becoming a bit more adamant on "this needs tests". Codebase had none after a year, and the other dev(s) are far more focused on code appearance than functionality. Fluent interfaces, "cool" language features, "conventional commit" message structure, etc are all prized. Sample data and tests? None so f…

yeah, that's the problem. If they don't get the value of what a test gives them, and then the focus shifts to aesthetics and conventions, etc... Personally when I review code, I look for the test, I need to find a way to tell me why that code exists and a proof that it works.

I had this issue with a different client about 6 months ago. I understand there are 'coding styles' that some companies stick with, and I'm not strictly opposed to them. I do bristle when I'm presented with the 'one true way' from devs who spend all their time on one project, or one tech stack, or one company. I jump around a lot, and multiple companies have "the one true way", and the conflict. Processes around commits, flow, commenting, etc - these vary more than some people would care to admit.

In response to that, I've started to care and focus more on tests and sample data to illustrate the core issues, changes and value for an issue. You want to change the code from 4 lines in to 1 4-line chained fluent interface to match other bits of the code, or to try out your new builder syntax? I really really have grown to not care too much - as long as I have some tests to demonstrate when something stopped working (or when our understanding of the project changed).

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

#112
Nope. We are judged by our individual performance by how many points (tasks) we complete. Since this has been a metric, unit testing is always an afterthought. During code checks / reviews, some developers will ask others to add unit tests, which will be hastily written.

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

#113
No. Doesn't agree with exploration.

I once had a job that didn't require any. I was given a specification and my job was to write some DSL-code of it. This would have been an excellent setup to practice TDD! Unfortunately, I wrote a script that basically translates the specification from word documents into DSL snippets and quit soon after.

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

#114
I mean I would "like" too, however, working in a startup there are some limitations. Our primary focus is building software that will improve metrics for our business, often time that software will only last a few months or so. As a result, it's just too costly for us to write tests for code that will only last a couple months most likely. Again, we would like to, and there are certainly some parts (small-parts) that have lasted for more than a few months and those don't have tests either, in that case, we "just never got around to it".

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

#115

Nope. I pretty much always find it to be counterproductive. Most of programming happens in the exploration phase. That's the real problem solving. You're just trying things and seeing if some api gives you what you want or works as you might expect. You have no idea which functions to call or what classes to use, etc. If you write the tests before you do the exploration, you're saying you know what you're going to fi…

> Be prepared to throw one away. You will anyway. Write a POC to learn then you can write tests first for production.

You are right, but technically speaking you already did implementation for POC without having the tests. So the full answer is "do POC without tests first, continue implementation with tests first".

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

#116

Almost never when writing new code. It feels like editing a manuscript before it even exists. If I "write" the code in pseudocode in my head or on paper first, I might write some tests first.

tests can be used to clarify your intentions and what you are attempting to build. It's best place when you are actually starting from a clean slate. Just write enough code to make the test pass, no more no less. Refactor and repeat. In recent years I've never written any new piece of code without test first and can not be any happier. Beside the confidence a test gives you, it really a great way to pin down your tho…

Yeah... I have read that and told that to others many times. But the thing is some of my best work (rare, though) has not been to spec. To any spec. More like, when you are doodling on a paper. "This might be a lake with a duck. Nope scratch that, it's actually a dragon and those are its scales. Yep."

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

#117

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 see you're aiming for the sainthood too. You don't also happen to help elderly women across the road, let everyone else out at a junction and never fail to say 'please' and 'thank-you'?

At the risk of being serious for a moment, saying "please" and "thank you" are two of the lowest effort, highest reward, and plainly decent things you can do as a human.

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

#118
In general I write unit tests as I implement. I would not write them unless I was convinced they were immediately beneficial to my productivity. Some parts of my code is not covered. In the beginning the test cases just outputs the result to standard out, in the end I would use assertions or disable them if they were dependent on external systems.

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

#119

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

And I feel glad I still have my mom and am calling her regularly.

Call your moms!

Post reply on HN