Hacker News new | past | comments | ask | show | jobs | submit login

Unit tests are insurance for later refactoring and library upgrades. This let's you avoid premature abstractions as you can easily swap out lines of code and verify you didn't break the app. This is especially important when you aren't the person doing the future refactoring.



Tests are insurance for refactoring which doesn't change the interface that is being tested.

Refactoring usually changes interfaces. Things are factored differently. The clue is in the name.

The higher up the stack your test is, the more insurance it gives you for refactoring. The lower downs the stack it is, the more likely it is to be thrown away or heavily rewritten after refactoring.


> Refactoring usually changes interfaces.

No, refactoring shouldn't change public interfaces. The very definition of refactoring is rewriting code without changing interfaces.

> Things are factored differently.

internally

> In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behavior.

https://en.wikipedia.org/wiki/Code_refactoring

You got the definition of refactoring wrong, please get it right, it's important. If you are breaking a public API, you are not refactoring anything.

Any piece of code meant to be private shouldn't be unit tested at all, only the behavior of a public interface.

Now internally you might call a third party lib, but that third party lib is then a separate "unit" itself.

I don't like the term "unit" because it's yet another word that is easily misunderstood and lost its original meaning with time.

unit testing should really mean "public interface behavioural integrity testing" or something like that.


What is external behavior? Behavior of the user interface of the program or behavior of some other code? What is external?

The term "refactoring" is commonly used in a way which includes changing of public interfaces. Random article, which even cites a book, agrees: https://thoughtbot.com/blog/lets-not-misuse-refactoring

If you're developing a library, then refactoring shouldn't change public interfaces. If you're developing an application and you own all the code paths to the code, then refactoring could change public interfaces, as the external behavior here would be the UI.

If you literally agree with the "refactoring shouldn't change public interfaces" then we need a new word for "code improvement which doesn't change external behavior, which can mean UI", which is the more commonly needed term.

And then perhaps we could agree that "code improvement often changes public interfaces" and how this relates to unit tests.


People expose too much of their code as a public api. The public interface should be small, and that's what the unit tests should test against.

If users should not access it directly, you don't have to unit test it directly, you test them indirectly via the public interface.


Exactly this. It's mostly lost on engineers that unit tests are usually testing at the layer that changes the most anyway - though the pain is felt once any real refactoring effort begins.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

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

Search: