Merl's Blog

TDD Part 3

Back at it again. They say learning is not linear. While climbing up a mountain, sometimes you must go down a bit to get to the next peak.

To make it clear I do see the incredible value in TDD. Writing tests before writing the actual code can help ensure that your code works as intended, is maintainable, and free of bugs. All of this is useful in general, but especially so in large projects. However, when you’re new to TDD, you might face certain difficulties, such as writing small tests for specific problems.

The Problem: Finding Correct Box IDs

Let’s consider a problem where you need to find two box IDs that differ by exactly one character at the same position in both strings. Given a list of box IDs, you’re tasked with finding the correct box IDs with this unique property.

# Here's a sample list of box IDs:

# abcde
# fghij
# klmno
# pqrst
# fguij
# axcye
# wvxyz

In this example, the IDs “fghij” and “fguij” differ by exactly one character in the third position. To find the common letters between the two correct box IDs, we need to remove the differing character, which results in “fgij.”

Of course in the actual challenge it was a long list of long strings. I will not provide this in the blog.

Challenges

Before, I was practicing TDD on problems I had encountered and solved before. I would erase them and write the tests and the code after. Following TDD principles to the T(DD). In this situation I was attempting to, then I reverted to my old ways.

Identifying the first and second test cases is not too bad. Then using the sample list provided above is straightforward as well. It is the middle that is challenging. The middle is complex which makes it challenging to keep the tests simple and focused. Fortunately I was still able to solve the problem, but like Uncle Bob said, “You are not done when it works, you are done when it’s right.”

Start with Simple Cases→ Work Incrementally → Use Descriptive Test Names→ Refactor as You Go

I will keep this in mind tomorrow.

Merl