Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A while back I described the benefits I think unit tests give you:

1. Well-tested parts

2. Decoupled design

3. Rapid feedback

4. Local context to test in.

At the same time, there are many cases where they don't help much. More here: http://henrikwarne.com/2014/09/04/a-response-to-why-most-uni...



I think you're missing the most important one: ensuring you don't unexpectedly break your own code in the future (when you come back in 6 months and forget why exactly it's arr[1:n-1] not arr[0:n-1] or arr[1:n]).


Sounds like a well placed comment would be of more value than a test there.


Comments are easy to miss though. We arnt perfect about going through our code and testing gives us objective measures.


A test won't say why code is the way it is; it will simply provide some sample input and expected output to that code. This may be enough, but not always.


They are also a word of encouragement to whoever sees your code for the first time because he needs to make a change in it, five years from now.

"Look, this code looks strange to you, you would have done it differently, and you are afraid you might break things you don't even know exist. But fear not, there are tests, they run, and they will guide you."

It's always relieve to find that some random old crap is actually well tested.


Not just to someone else. Nothing is more pleasant than going in to refactor code you wrote years ago, and find that you wrote comprehensive unit tests. Thank you past-me!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: