don't worry, it's probably fine

Coding exercise: Build a stopwatch

04 Nov 2020

nablopomo tdd

Out of curiosity, I had a go at test-driving the design of a stopwatch.


"_IGP1485-stopwatch" by spbatt is licensed under CC BY-NC-ND 2.0

A stopwatch should have the following capabilities:

  • It can start, and begin the timer.
  • It can stop, and pause the timer.
  • It can reset, and set the recorded timer to zero.
  • It can take a lap, which records the difference between the current time and the last lap taken.

The task is to implement a stopwatch with four methods, corresponding to the four capabilities mentioned. Additionally, there should be a display() to show the display on the stopwatch.

The display should be in the format:

Current Time: mm:ss
Lap 1: mm:ss
Lap 2: mm:ss
Lap n: mm:ss

Some questions to answer:

  • How would you do this in a purely functional way?
  • How would you do this using only immutable data structures?
  • What constructs best represent the user interface of the stopwatch?

Have fun, and if you try this yourself, please tag me so that I can see!


Default output

Current Time: 00:00

Taking Laps


// wait one second
// wait by two seconds


Current Time: 00:03
Lap 1: 00:01
Lap 2: 00:02

November is National Blog Posting Month, or NaBloPoMo. I’ll be endeavouring to write one blog post per day in the month of November 2020 - some short and sweet, others long and boring.