by aazaa · 6y ago · 0 comments
Without a suite of unit tests, there's no way to know that your refactor hasn't introduced bugs of some kind. A suite of tests won't necessarily catch every bug you'll introduce, but it will catch many of them. I've seen this repeatedly.
I've had many conversations with developers who claim they don't need unit tests. When I ask "How do you refactor?" the answers is usually some variant of "I don't."
But, I think this distinction gets blurrier the larger your organization. While you may have intended some class in your web app as an implementation detail, the larger your organization is, the more likely it is that somebody will start using that class for some other purpose. And thus and implementation detail becomes an interface that other people depend on.
When the layer is essentially pro forma and has no logic of its own, this is excruciating, but the line coverage number rules all.
Let's say you have controller -> Business logic layer -> Data access layer.
The controller takes in some input, validates it, passes it to the business logic layer. The business logic layer retrieves some data from the DAL, does some logic, maybe writes data back to the DAL, and passes the data back up to the controller. The problem is that the business logic layer is usually doing two things at the same time, in the same function: the 'plumbing' (calling other things and passing data round), and the actual business logic.
When you try and define a boundary around the code, it gets messy, because what you want to test, the business logic, is mixed in with the plumbing so you have to use either dependency injection or mocking to force a boundary. This makes your tests more brittle as changes in things you don't want to test (your dependencies) affect your tests.
If you pull out the business logic into its own modules, and instead have a function that orchestrates the business logic (i.e. tell the database what data you want, tell the business logic functions to perform an operation on the data), you end up with a nice clean boundary around your business logic, which can be unit tested easily without mocks.
You then test the 'plumbing' part by using a component test that checks that the inputs and outputs of the component as a whole (business logic + plumbing + data access) are correct.
Use the best tool (type of test) to document & test the business or technical rules being requested.
Tests help teams (and future-self of the individual who wrote the code) understand why certain code was implemented & what purpose it is meant to solve.
As the business changes the rules they want implemented, having well written out tests seem to help significantly with handling those rules. It also helps when you want to refactor for the sake of code understanding & readability.
It is often the case where both developers & those requesting the changes forget why some of the rules were put in place to begin with.
Agreed. I think that in many cases it is a signal that your code is too complex, and may be violating the Single-Responsiblity principle.
That said, as someone who has spent the majority of his career writing dynamically typed code I can definitely recall far more instances where statically typed code has run and worked first time vs times when I've written some code with unit tests and it worked first time.
Start off with things clean, consistently try to keep it that way, and it's actually pretty easy and not too much effort.
Start off with a mess, or start off clean but then neglect things for a while, and you will wind up at the bottom of a hole that it is going to be a long, painful slog to dig yourself out of.
This applies to tons of things: coding, keeping your bathroom clean, car and home maintenance, personal relationships, financial management and record-keeping, etc.