Sunday, June 19, 2011

The Incident at the North Gate (or Why = Is Not ==)

In many modern programming languages the expression "a = b" is used to assign the value b to the variable a. In contrast, the expression "a == b" tests for equality and returns true if and only if the values of a and b are the same. Unfortunately, in some languages, such as C and C++, this syntax can lead to difficult to detect errors because the = operator returns the value being assigned. For example, "if (a = b)" will compile without problems and trigger if and only if b's value is equal to true.


King Fredrick loved to tell the tale of the "incident at the northern gate". He was a firm believer in the importance of clear, correct documentation. And, the occurrences of that day provided a perfect case study.


The incident had occurred during the last period of war and unrest. King Fredrick's father held the throne and was working hard to guide the kingdom toward peace and prosperity. As part of this effort, he personally oversaw the creation of new rules for the castle guards.


The new rules read:

"Check the entrant's ID and examine their entrance ID class.

IF (ID class == 0): they are a member of the royal family, so let them pass,

ELSE IF (ID class == 2): they are a member of the carpenters' guild, so let them pass;

ELSE IF (ID class == 3): they are a member of the plumbers' guild, so let them pass;"

and so forth. Unfortunately, even a king makes syntax mistakes occasionally. The erroneous line read:

"ELSE IF (ID class = 72): they are a mime, so stop them"

The king had used an assignment statement instead of an equality test.


It took three days for someone to reach this test in the series of IF-ELSE statements. On that day a young hedgehog walker (ID class 185) arrived at the north gate with the King's prize hedgehog. He presented his ID to the guard. The guard consulted his manual and paused in confusion.


"What is wrong?" asked the boy.


"Well. The manual says here that I am to change your ID class to 72 and prevent you from entering." The guard responded. As he spoke he reached into his booth and pulled out a permanent marker. He crossed out the ID class of 185 and wrote 72 in its place.


"But, I am supposed to go inside." argued the boy. "I have the king's hedgehog."


"I am sorry lad. No mimes allowed in here!" responded the guard.


The boy looked confused. "Mime?"


"Yes. It says ID class 72. That means you are a mime!" answered the guard.


"But, you just wrote that. It was ID class 185. I am a certified hedgehog walker. And, I am working on my parrot walking license too. I provide a valuable public service."


"The manual told me to reclassify you as a mime, so that is what I did. And, we do not allow mimes here!" responded the guard formally.


"But…" started the boy.


"Be gone!" shouted the guard.


The boy quickly retreated and circled around to the south gate. Unfortunately, his ID now clearly indicated that he was a mime. He was not allowed to enter. Discouraged, the boy went home, bringing the king's hedgehog with him.


The incident went unnoticed for almost a day, until the king sent a search party to find his hedgehog. Fifty knights arrived at the boy's home, scaring both the boy and the hedgehog out of their wits. It took three hours for the boy to pull himself together enough to explain what had happened. And, it took another five hours to calm down the hedgehog.


The king was furious when he found out. He marched down to the north gate and grilled the guard on why he had changed the boy's ID. The guard pointed to the instruction and explained himself. But when the king saw that this was his own mistake, his mood only worsened. The king launched into a six hour rant and then immediately insisted on three sweeping changes:

1) All IF statements would put the constant value before the variable (e.g., 0 == Class ID), so that no assignments could happen by mistake;

2) Every new set of rules had to be checked by at least one other person for correctness (although this was a great practice in its own right, the king also wanted someone else to blame if there was a mistake).

3) All new rules would be unit-tested by a group of Shakespearean actors.

Despite the seeming overhead of these new regulations, the castle was launched into an unexpected period without any mistakes. Even the castle stables, which had 100 page rulebooks, went a full week without any mistakes.


Feeling satisfied with his contribution for the day, the king decided to get a pet parrot. He had heard good things about parrots recently.


-------------------------


Related stories: Also see the story of how King Fredrick rewrote the instruction castle instruction manuals to use the Switch/Case format in Switch/Case and the Palace Guards. Or read more about the IF-ELSE statement in Learning IF-ELSE the Hard Way.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.