Member-only story

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…

--

--

Alonso Del Arte
Alonso Del Arte

Written by Alonso Del Arte

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

Responses (1)