Use TDD for Faster Development
Every engineer *knows* they should be adding tests to their work, but most of us, it’s one of those things that gets “cut for time.”
If you’re on any sort of deadline (which, yeah) then this pressure is totally understandable. You don’t get more points from the customer for writing tests to make sure something works. They only care if it works!.
The usual argument says adding tests is a way to prevent future problems. Spot on. However, there’s another reason you should be writing tests that pay off now instead of in the future.
Writing tests before you start helps you work faster than writing no tests at all.
What is Test-Driven Development?
Test-driven development, or TDD, is the process of writing a series of tests for your code to pass before you’ve even written the code.
It goes like this:
- Write out tests your code needs to pass
- Code your function/page/API
- If it passes, move on; if not, go back a step
- Then, look at your next test
- Add or alter your code to accommodate it (if it doesn’t pass already)
- Test again
- Repeat until all tests pass
In contrast, here’s the “process” that most of us follow if left to our own devices:
- Build a function
- Run your program
- Does it work? Cool. Maybe you try it a few more times, throw in a null value or something as a kind of “QA-lite.” But it’s not a standard, it’s more of a “oh yeah, let’s add that.”
- Ship it.
At a glance, the first path looks more tedious. Why do 8 steps when you can do 4? That’s just common sense!
However, while it looks like the TDD path takes more time than business-as-usual, it’s generally the opposite. TDD gives you clarity in your direction, a way to measure success, and a final, “shippable” point (the passing of all predefined tests) that prevents scope creep.
An example? Sure.
You are developing an API. Your API will be a GET request taking in a user’s ID and authentication token and will return the 10 latest photos from their profile. It will also record the time and date of the request to a database.
Right off the bat, there are a handful of error conditions I can think of:
- Request is made without user ID
- Request is made without authentication token
- Request is made with the wrong auth token
- The request is fine, but the photos can’t be found (DB error)
- The request and retrieval work, but there was an error writing the event to your query-history DB
- The user doesn’t have 10 photos to retrieve
These are fairly obvious needs to handle, yet if one doesn’t take the time to write them out ahead of time, it would be incredibly easy to overlook these in your development process. This is a problem because your QA or PM will catch these and send your work back to you. It will take twice as long to “fix” the problem than it will be to develop it in a test-driven manner from the get-go.
How to make friends and influence (QA) people
If you have a QA team, ask them for the 10 most common checks they typically apply to your code (or your team’s code). Write tests for the checks (or get close).
Better yet, ask them for the 10 most common errors they find in pre-shipped code. Write tests for those, and become their favorite engineer. Now that you’re their favorite engineer, you may find that your code gets priority when there are eight different PRs out for them to review because they know yours will be the least headache.
Be a team player
Create a basic template for you and your team to use, a simple file you can copy and paste and run your code through. For every new function, page, endpoints, etc. you now have a rough set of criteria it needs to pass in order to even get a PR made (based on what you learned from your QA folks). This doesn’t have to be a hard-and-fast rule, just something to serve your team in the process of development.
If your team doesn’t have a culture of testing, you may find it hard to make that shift. A framework like TDD makes their work faster and easier, and it’s hard to say no to that.
What if my team doesn’t have QA or coding standards?
Then it’s time to create some. Start now, even if it means starting badly. Or contract a QA for a month to look over your shoulder, learn your product, and ask the insightful questions you’re missing, then translate those questions into tests so you can make TDD a part of your workflow.
The last word
TDD is like sharpening the saw. It may not seem “productive,” so you may have a mental block about making time for it, but it absolutely makes up for the up-front cost by increasing your rate of productivity.