liveBook · Manning

Code spike

As explained in Test Driven: Practical TDD and Acceptance TDD for Java Developers

A spike solution is an exploratory prototype used to investigate a potential solution and estimate its feasibility or effort.

A code spike is a short, exploratory experiment for finding out whether a potential solution can work and how much effort it might take. Its goal is to make an unknown familiar enough to decide whether to continue or try another approach. section 3.0 section 3.1.1

Keep the experiment focused on learning:

  • Probe the uncertain part, using sketches, temporary code, documentation, or small tests. section 3.1
  • Treat the result as knowledge, not production code. Use what you learned to write production tests first, then implement a lean solution rather than copying the prototype. section 3.1.1 section 3.1.3

Learning Java regular expressions

To investigate parsing template variables, test Java’s regex API on “The needle shop sells needles.” A learning test can check that matching finds both occurrences and returns their start and end indices. Removing parentheses from the pattern and rerunning the test reveals whether those capture groups matter. The useful result is knowing how to locate the matches—not keeping the spike code. section 3.1.3

Listing 3.1. Making sure we can locate all matches within a given String
@Test public void testFindStartAndEnd() throws Exception { String haystack = "The needle shop sells needles"; String regex = "(needle)"; Matcher matcher = Pattern.compile(regex).matcher(haystack); assertTrue(matcher.find()); assertEquals("Wrong start index of 1st match.", 4, matcher.start()); assertEquals("Wrong end index of 1st match.", 10, matcher.end()); assertTrue(matcher.find()); assertEquals("Wrong start index of 2nd match.", 22, matcher.start()); assertEquals("Wrong end index of 2nd match.", 28, matcher.end()); assertFalse("Should not have any more matches", matcher.find()); }

A compact learning test checks both match positions and that no further matches remain. section 3.1.3

How Test Driven: Practical TDD and Acceptance TDD for Java Developers covers it

How deep it goes
Defines the experiment and walks through regex learning tests, while treating prototype code as disposable rather than production-ready.
Examples
Tests Java regex matches, their start and end indices, and whether capture groups affect matching.
What you can do after
Evaluate a potential solution by exploring an unfamiliar API and using the resulting knowledge to guide implementation.
How it connects
Connects spikes to test-driven development, learning tests, and Java regular expressions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Test Driven: Practical TDD and Acceptance TDD for Java Developers. The section links point to the parts of the book it draws on.