Live data from Hacker News

Ask HN: Do you write tests before the implementation?

news.ycombinator.com

71–80 of 329 posts

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

#71
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 far (up until I started last week).

I've had push back on my initial contributions, and I keep saying "fine - I don't care how we actually do the code - change whatever I've done - just make sure the tests still run". All I've had is criticism of the code appearance, because it's not in keeping with the 'same style' as before. But... the 'style' before was not testable, so... there's this infinite loop thing going on.

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

#72

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 thoughts and write what makes them meterialize.

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

#73
post #30

Earlier quoted context omitted.

The benefits you describe seems to be achievable with tests written after code as well. We write extensive unit tests, but mostly after development work. The re-write work you mention is then avoided.

The benefit of TDD is that the code you end up with will actually be testable. Just keeping in mind that you have to write a test for your code, changes how you write it. As a bad example, imagine having a 1000 line function that just does everything you needed for the new feature... Good luck testing that afterwards.

> Just keeping in mind that you have to write a test for your code, changes how you write it.

Which is often enough to ensure the code is testable.

Generally, I'll write some tests sort of alongside, or soon after (like, a couple hours or a day) to not lose the initial thought process. Going back to code days/weeks later and trying to 'test' it when it wasn't conceived of as testable is tough.

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

#74

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.

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

#75
post #21

The majority of the time, no. There are a couple of circumstances I often do, though. The first is when fixing a bug - writing the (red) regression test first forces me to pin down the exact issue and adds confidence that my test works. Committing the red test and the fix to the test in two commits makes the bug and its fix easy to review. The second is when I'm writing something high risk (particularly from a securi…

> Committing the red test and the fix to the test in two commits makes the bug and its fix easy to review. I've done this in the past. Then I started to use `git bisect` and having a red test somewhere in your commit-history is a killer for bisect. So now I tend to include both, the test and the bug-fix, within one commit.

I get around this by squashing the PR commits. Reviewers see the individual commits, and good CI means they can see the test-commit failed and the fix-commit passed, but post-merge it's a single passing commit.

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

#76
Normally no. I do not do TDD either. If however I am writing some complex algorithm where I know that I'll make few bugs here and there I would write test.

Being too proper, doing everything by the book does not always translate to better code or good ROI

Also being an older fart and programming for so many years I am usually pretty good at not making too many bugs anyways.

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

#77
99% of the code I write is test first. It makes my life easier - I always know what to do next and it reduces the amount I need to keep in my head.

TDD done the way many developers do is a PITA though. When I write a test it will start off life with zero mocking. I'll hit the db and live APIs. From here I'm iterating on making it work. I only introduce mocking/factories because it's harder work not to. I'll gradually add assertions as I get an idea about what behaviour I want to pin down.

Done this way using tests is just making life easier, you can start off testing huge chunks of code if that's what you're sketching out, then add more focused tests if that's a faster way to iterate on a particular piece. For me the process is all about faster feedback and getting the computer to automate as much of my workflow as possible.

edit: Kent Beck had a fantastic video series about working this way, I can only find the first 10 mins now unfortunately but it gives you a taste, https://www.youtube.com/watch?v=VVSSga1Olt8.

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

#78
post #67

No. And also 'do you write a test for everything?'. Also No. Tried it, ended up with too many tests. Quelle surprise. There is a time/money/cognitive cost to writing all those tests, they bring some benefit but usually not enough to cover the costs. I'm also going off the 'architect everything into a million pieces to make unit testing "easier"' approach. I heard someone saying that if you write a test and it never f…

Do you consider coverage an effective metric? I've got some code that has a test suite which is effectively a bunch of low level driver checks, plus a bunch of common example snippets and checks for eg empty inputs etc. Coverage gives an idea of how many lines of code have been run, but obviously no guarantees of correctness for those specific lines (eg you can't detect a double negative). It's worked well for me so…

I would agree that "coverage gives an idea of how many lines of code have been run, but obviously no guarantees of correctness for those specific lines"

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

#80
I usually write an implementation first, but when testing it i try to re-evaluate the requirements - i do not look at implementation at all. Test should handle all edge cases.

And i do modify tests, because sometimes assumptions are wrong(or domain experts change their mind or got something wrong)

Sadly functionality requirements are very soft in my industry(once it was requested from me to do a perfect fuzzy match..)

Most of the time i am required to modify untested legacy code, that starts with test(if it is even possible).

Post reply on HN