programming, just for fun,

The sentinel toothpaste: a useful programming technique for everyday life

Adam Dunkels, PhD Adam Dunkels, PhD Follow Nov 18, 2022 · 4 mins read
The sentinel toothpaste: a useful programming technique for everyday life
Share this

Here’s one programming technique that I found very useful in daily life: the sentinel value. I use it pretty much every day – to manage toothpaste.

In programming, a sentinel value is a simple way to keep track of the end of a list of items. Lets say you have a list of items. You make the last item in that list a special one. That way you easily know that you have found the end of the list: when your item is that special one, that’s the end of your list.

Here is a visual repressentation using emojis:

😀 🥰 😎 🤗 🙄 😜 😮‍💨 🐈

The cat is not a smiley face, so we can use the presence of the cat to know that we have reached the end of the list.

One typical usecase for this programming pattern is null-terminated text strings, where the null character \0 is used to mark the end of the characters in the string. To compute the length of the string, we just need to go through the string until we find a null character. That null character is the sentinel value.

Here is the string String, with the final \0 character being the sentinal value:

S t r i n g \0

Another usecase is a null pointer value in a linked list. If each member of the list is a non-null pointer, we can use the presence of a null pointer to indicate the end of that list.

I have found this programming technique extremely useful in daily life, to manage toothpaste tubes.

It isn’t fun to run out of toothpaste when you need it the most. You don’t want to find out that you’re out of toothpaste just when you need to brush your teeth before leaving the house.

So when I buy toothpaste, I buy a bunch of them, and always make sure to get one tube of toothpaste that is different from the others – the sentinel toothpaste:

The sentinel toothpaste is that last tube, which is different from the others

When I find that the only tube of toothpaste left is that special, sentinel toothpaste, then I know it is time to buy more toothpaste. And then I have ample time to do so.

I first learned of the sentinel object technique in a programming lecture by Lennart Andersson, who was one of my first lecturers in computer science and programming and a great computer scientist and teacher. I learned a bunch of extremely useful programming techniques from Lennart, which I have had a lot of use in my professional life, but this is the only one I found useful in life outside of computer science and programming.

Here are a few of the useful things I learned from Lennart:

  • How to efficiently write parsers for formal languages, such as mathematical expressions and programming languages.

  • How satisfyingly compact functional programming techniques can be, particularly with map, reduce, and filter operations, even in imperative languages.

  • That (some) printers weren’t just dumb devices that would print bitmaps, but that they were running a fully-fledged language called PostScript. So you could manually write a piece of code that you could send to the printer, which would run it and generate output – on the paper. What a realization!

  • How to avoid comparing floating point values directly with each other – instead, subtract them from each other and check that the difference is small. That is, instead of checking a == b, check abs(a - b) < 0.0001 (or some other small quantity). This is a nifty way to protect against small numerical errors in floating point repressentation.

  • The importance of external vs internal repressentation of data structures: we were given a lab assignment to write a small Excel/VisiCalc clone. We were free to do it in any way we wanted, with one exception – we were not allowed to use a two-dimensional array to repressent the contents of the sheet. The purpose was to teach us that the externally visible repressentation (a two-dimensional sheet of cells) was in no way related to the best way to repressent the same data internally, inside our program. This has stuck with me ever since.

Adam Dunkels, PhD
Written by Adam Dunkels, PhD
Who am I?