Palindrome checker is a better basic example of test-driven development

Alonso Del Arte
8 min readNov 25, 2019
Photo by Filip Mroz on Unsplash

A commonly given basic example for test-driven development (TDD) is a function that is supposed to add up two integers. The stub always returns a specific number, like 0, instead of actually adding up its two parameters.

Of course that fails a test that expects something like 1 + 1 = 2. So you change the stub to return 2. But now that fails a test that expects something like 2073 + 125 = 2198. So you change the function to return a + b.

If people conclude from this that TDD is dumb and pointless, can you blame them? It’s an example simple enough that anyone with a computer can follow along, but it doesn’t do anything to convey the sense of accomplishment one can feel in making a previously failing test now pass.

A better example, I think, would be the palindrome checker. We’re going to use a Java IDE (like NetBeans or IntelliJ) to write in Java a command line palindrome checker utility.

It’s barely more useful than wrapping plain old addition in a new function, but I think the palindrome checker TDD exercise would do much more to convince TDD skeptics that TDD’s the way to go.

Also, the palindrome checker exercise provides more opportunities for follow-up exercises than trying to reinvent the wheel on basic integer arithmetic.

Although this example is going to be specifically in Java, I think you can follow along with almost any other programming language with a reliable and established testing framework.

The command line tool will take in the command line arguments as an array of String instances, and report which ones of them are palindromes and which ones are not.

A palindrome is a word, phrase or number that reads the same backwards as forwards. For example, “kayak” and 121 are palindromes, “canoe” and 122 are not (122 backwards is 221, and “canoe” backwards is not even a valid word as far as most spellcheckers is concerned.).

So our palindrome checker will have a standard public static void main(String[] args) procedure which puts each of the args through a Boolean function that ought to return true for palindromes only.

--

--

Alonso Del Arte

is a Java and Scala developer from Detroit, Michigan. AWS Cloud Practitioner Foundational certified