dev@whisperer:~$

PL

The Elephant in the Room (in Cairo)

In programming, it’s easy to brush against infinity. We still don’t know whether or when a Turing machine will halt. A missing comma or an unclosed parenthesis can cause a program to fall into an infinite loop from which it may never escape.

The Elephant in the Room

There are many ways to defend yourself against infinity. One of them is to place a guard in your code — a sentinel. A sentinel value is a special value that signals a program to terminate a loop or stop an algorithm.

Let’s translate that into a real-world example. Imagine we’re writing a simple message parser. To better illustrate the role of a sentinel, let’s assume the parser keeps reading the message character by character in a loop and stops only when it encounters a special end-of-message marker - in our case, the hash character (#).

In code, this could look like this:

def read_message(message):
    sentinel = "#"
    parsed = ""
    index = 0

    while True:
        char = message[index % len(message)]
        index += 1

        print(f"Read: {char}")

        # Comment out these three lines and the
        # program will keep reading forever.
        if char == sentinel:
            print("\n[STOP] Sentinel reached.")
            break

        parsed += char

    return parsed

message = "Hello World#"

print("\nParsed message:")
print(read_message(message))

Running the program produces the following output:

Parsed message:
Read: H
Read: e
Read: l
Read: l
Read: o
Read:  
Read: W
Read: o
Read: r
Read: l
Read: d
Read: #

[STOP] Sentinel reached.
Hello World

If you comment out the entire if block, the program will fall into an infinite loop, reading the characters of “Hello World” over and over again until it’s stopped. With the sentinel in place, it terminates as soon as it encounters the first hash.

There’s a catch, though. The example only works because the hash appears exactly where we expect it to — at the end of the message. If it never appeared, the program would remain stuck again in an infinite loop.

But what if we replaced Hello World with a message in which the hash is a perfectly legitimate character? For example:

message = "John loves C# but he loves Python even more."

For this input, the program prints:

John loves C

I don’t know what John would think about that, but something has clearly gone wrong. The culprit isn’t the algorithm — it’s the programmer, who chose an ambiguous sentinel value. The guard they hired turned out to be a little too eager and refused to let the guest of honor into the party.

That’s why a sentinel value should be truly unique. If the character you’ve given a special meaning can also appear as legitimate input (such as the # in the name of the programming language C#), it can lead to subtle and hard-to-diagnose bugs.

So when you’re choosing a guard, make sure it’s a truly unique and trustworthy value.

The Elephant in Cairo

But why an elephant? And why Cairo?

The earliest known mention of it appeared almost 40 years ago in Byte magazine (1989). You can find a scanned copy here (page 428 of the PDF, page 404 in the original magazine).

In the article, Peter C. Olsen humorously describes how members of different professions would go about hunting an elephant in Africa. Olsen also describes experienced computer programmers, who ensure that their elephant-hunting algorithm terminates by placing a known elephant in Cairo — a classic example of a sentinel value.

In the original text it looked like this.

Computer scientists hunt elephants by exercising Algorithm A:

  • Go to Africa.
  • Start at the Cape of Good Hope.
  • Work northward in an orderly manner, traversing the continent alternately east and west.
  • During each traverse pass
  • Catch each animal seen.
  • Compare each animal caught to a known elephant.
  • Stop when a match is detected.

Experienced computer programmers modify Algorithm A by placing a known elephant in Cairo to ensure that the algorithm will terminate.

Assembly language programmers prefer to execute Algorithm A on their hands and knees.

Reprinted from “Byte”, Vol. 14, No. 9 (1989).

As a little writing exercise, I tried to imagine a few modern IT roles that might have found their way into Olsen’s article if he were writing it today. Here are a few ideas that came to mind:

Scrum Master sets the goal of catching an elephant during Sprint Planning, then spends the next two weeks asking every day whether the elephant-catching progress is on track. At the end of the Sprint, they run a retrospective to discuss why the elephant was not caught.

DevOps Engineer sets up the infrastructure for catching the elephant. During deployment, it turns out that the elephant image in the container doesn’t match the elephant version defined in Terraform.

Vibe coder prompts a newly released model to catch an elephant. After burning through a few million tokens, the model generates an image of a rhinoceros with a trunk and five legs.

Cybersecurity Engineer sets up a firewall to catch the elephant. The elephant simply walks around it.

BI Developer writes a query and marks the elephants’ current locations on a dashboard. When they arrive at the indicated spots, the elephants are nowhere to be found.

L1 Support creates a ticket for catching the elephant and assigns it to a random person.

Funny or just painfully lame? Either way, come up with your own better ones.

From: peer@diku.dk (Peer Wandel Hansen)
         .    _.----~~~~~~~7
             /              ~-..-~~--..--.
       .'.'.'                             `.
         .~                                 \
       .'                                    `.
   .   (                                       \
 '.    )                                        `.
   '  (                                           ~-.
       \                                             ~-~~7
        `.       __.._                                  .'
          ~-.--~~     ~--.                             /
                         ;                          .-~
                         (                        .~
                          `.                    .'
                            ;                   ;
                            `.                  `       _
                             )                   )     / )
                            (                 _.-'  .-' .'
                            `.               (      )   /
                              7             _;      < _/
                               \           /         ~
                                \         /
                                 `. __..-'
                                   ~

This ASCII pic can be found at https://asciiart.website/art/3723