Sunday, July 30, 2023

Don't Waste Your Gold: The Technique Most Software Developers Miss Out On

Test-Driven Development (TDD) is a software development methodology that guides the creation of software like a sculptor casting a statue, methodically refining the object until it embodies the desired quality and performance. While TDD may seem intimidating to some, let's demystify it by comparing it to the art of casting.

Casting is a sculpting process where you start with the object you want to replicate and build a mold around it. Alternatively, you can begin by carving a mold and then filling it with the material you wish to craft your object from. The process iteratively escalates, gradually increasing in scale or improving the material.

Suppose your goal is to create a gold statue of an elephant. You'd first carve the elephant out of a less expensive material, say wax. A sand mold is made and a low-cost liquid metal is poured in, replacing the wax. Now, you've got a cheap metal replica that you can further refine by polishing. The subsequent step is to create a high-quality, reusable metal mold for the final golden statues.

TDD follows a similar iterative process. Here, the set of tests acts as the mold, defining the shape of the software product, while the software product is the sculpture you're continuously improving. You begin with a few tests you think are accurate and build the software to pass these tests. Then, through manual testing, you uncover corner cases that fail.

More tests are added to handle these edge cases, and the software becomes increasingly general until there are no more failing corner cases. Now, you have a system that's functionally correct, analogous to the cheap metal figurine, as it aligns with the tests or molds. However, it might not yet be 'golden'.

To polish your software, you improve its performance, replacing portions of the code with faster algorithms. For example, instead of an algorithm that exhaustively tries all possible combinations, you may leverage domain knowledge to provide a more efficient solution. This could be a mathematical solution that's computed instantly from a formula, or if the solution space is concave, you can try gradient descent.

The tests, or the mold, ensures the improved algorithm is functionally identical to the prototype, but faster, just like the gold elephant is a better version of the cheap metal one.

If you are a non-programmer reading this, everything may seem logical. You might assume this is the universal method of crafting software, mirroring dressmaking patterns, architectural blueprints, or cooking recipes that guide their respective crafts.

However, it might surprise you that most developers do not follow TDD, nor do most development teams practice the more encompassing Behavior Driven Development. Over the last 20+ years of interviewing candidates, I've found that while most have heard of these methods and acknowledge their logic, they often don't employ them.

Why? Simply put, it's tempting and convenient to start coding immediately. In gold casting, there is the risk of wasting expensive gold. With software, the erroneous perception is that there's no such risk. However, this way of thinking is a mirage. Bad software breeds bugs, leading to rework, and rework consumes time – a resource more valuable than gold.

So, next time you're embarking on a new software project, consider following the golden path of TDD. Create your 'molds', cast your software, refine it, and make it golden, and remember, your time is the most precious resource you have.
sources: