Testing That One Thing
Happy Wednesday! We're talking this week about how to write better code faster. You might want to catch up here and here.
Today we're talking about step 2, Testing. That doesn't necessarily mean automated tests. Testing is the step where you evaluate whether the code you wrote in step 1 is doing the thing that you want it to do.
We've made this easier on ourselves in step 1 by first breaking down our feature into discrete chunks with well-defined boundaries. Our task here is literally to evaluate "When I do X, do I see Y?". If the answer is yes, the test passes. For instance, "If I make a post request with this payload, do I get back a response with this body?"
Well-defined boundaries serve as checkpoints where you can inspect the flow of data through your application. You know unambiguously what the inputs and outputs to a particular part of your app should look like, and you can quickly determine whether they are performing correctly.
The question of course is "How do I send this HTTP request without setting up the whole feature, or at least a skeleton of it?". There are a few ways. You could fire up Postman. You could write little script or a simple page. But if you really want to tighten up your virtuous loop, you'll write an automated test. It takes a minute or two to write the test, and then it provides immediate feedback continuously as you work. No need to switch windows, refresh pages, or manually inspect values. Hand all that tedium off to the computer and get back to the fun part of coding. Plus, as your app gains complexity, the test remains to verify that the endpoint is still working.
Spend a day or two and learn the basics of writing automated tests in your language of choice. It will pay off.
Tomorrow, on to debugging!
Next Up:
Debug early, debug often.
Previously:
One Thing at a Time