Writing Tests Before Code Is Insane (Until You Try It)

Read full text Summarise with VIRAT
Authors: Daniel Rosehill, Gemini 3.1 (Flash), Chatterbox TTS
Cited by: 0

Summary

No summary yet. VIRAT can read this paper and write a plain-language summary of its aim, methods, key findings and limitations.

Summarise with VIRAT Free to try with your Science Catalogs account.

Abstract

Episode summary: That "one-line change" that broke your entire app isn't magic—it's the cost of flying blind. This episode explores why unit testing is a non-negotiable best practice in 2026, debunking the myth that it slows you down. Learn the "Arrange, Act, Assert" framework, how to start with just one function, and why writing tests before code might be the sanity check your workflow needs. Powered by Google Gemini 3 Flash. Show Notes The Haunted House of Code It's a universal developer jump-scare: you change a single line of code—maybe just a padding value or a string label—and suddenly the entire login flow collapses. It feels like haunted house logic, but it's usually just the result of flying blind. Without a safety net, every change is a potential landmine. This is where unit testing enters the conversation. While many developers view it as a burdensome "tax" on development speed, the reality is that it's an investment in velocity. The core idea isn't to simulate the entire internet, but to test the smallest possible unit of code: a single function or component. By isolating it from databases, networks, and file systems, you can run a controlled experiment to see if the logic holds up. The "Arrange, Act, Assert" Framework To structure these tests, the conversation highlights a simple, three-step methodology: 1. **Arrange:** Set up the conditions. Define your inputs (e.g., X = 5, Y = 5). 2. **Act:** Call the function you are testing (e.g., result = add X and Y). 3. **Assert:** Verify the outcome. Check if the result equals 10. If it does, the test passes; if not, it fails immediately, catching the bug before it ever leaves your machine. This process codifies expectations. Instead of manually clicking through an app to see if a button works—a slow, error-prone method—a unit test performs that check in milliseconds, every single time. The Economics of Quality The financial argument for testing is staggering. A 2020 study by the Consortium for Information and Software Quality estimated the cost of poor software quality in the U.S. alone was over two trillion dollars, largely due to technical debt and preventable bugs. The cost of fixing a bug scales exponentially with time: seconds to fix while writing code, hours in staging, and potentially catastrophic costs in production involving emergency patches and data recovery. Beyond the financials, there is a psychological benefit. A solid suite of tests acts as a safety net, allowing developers to refactor complex code with confidence. If the tests remain green after swapping out an algorithm, you have proof that the behavior hasn't changed. Writing Tests as a Diagnostic Tool Perhaps the most valuable insight is that the act of writing tests improves code architecture by default. If a function is difficult to test, it's often a sign that the code is "tangled" or poorly designed. For example, a function that calculates a price while simultaneously calling a database, sending an email, and checking an API is a nightmare to test. This forces developers toward "pure functions"—logic that takes an input and returns an output with zero side effects. By breaking a giant, thousand-line function into smaller, focused pure functions, the code becomes more readable, modular, and maintainable. It's a virtuous cycle: you start writing tests to catch bugs, and you end up with better-designed software as a byproduct. Starting Small: The Practical Steps For those staring at a legacy codebase with zero tests, the advice is to start small: 1. **Pick One Function:** Choose something clear and isolated, like a currency formatter or email validator. 2. **Install a Runner:** Use tools like Jest (JavaScript) or Pytest (Python). These run locally on your laptop and require no complex infrastructure. 3. **Create the Test File:** Name it parallel to the source file (e.g., `format-currency.test.js`). 4. **Write the Test:** Import the function, write the test block using Arrange, Act, Assert, and run `npm test`. The "Pro Move" is to try to break the code. Pass `null`, negative numbers, or invalid strings (like "banana") to see how the function reacts. This hardens the code against edge cases that inevitably appear in production. For messy, legacy codebases, "Mocks" and "Stubs" allow you to fake external dependencies (like a database) so you can test the logic without needing the full infrastructure running. Finally, the conversation touches on Test-Driven Development (TDD), a methodology where you write the test *before* the code. While it sounds counterintuitive, it forces you to define exactly what the code should do before writing it, often resulting in cleaner, more focused implementations. Whether you're testing after or before, the goal is the same: stop flying blind and start building with proof. Listen online: https://myweirdprompts.com/episode/unit-testing-tdd-legacy-code

Fields of Study & Subjects

Software Testing and Debugging Techniques Software Engineering Techniques and Practices Software Engineering Research

Publication details

Journal / source
Zenodo (CERN European Organization for Nuclear Research)
Publication date
30 March 2026
Language
EN