Thursday, August 25, 2011

Data Validation, Marcus, and the Cheese Minstrel: Part 1 of Marcus and the Cursed Cheese

Data validation is an important practice to ensure the security and robustness of a program. In short, data should be checked to confirm that it is valid (e.g., no null pointers), non-malicious (e.g., no escape characters in input strings), and of the expected form. Data input into the program should be validated to prevent invalid or malicious data from causing undesirable behavior. Even within the program, data coming into functions should often be validated to protect against accidentally passing a bad value.

It was by pure chance that the wizard Marcus ran into the Minstrel of Cheese. To be certain, the Minstrel of Cheese did not look like a typical minstrel. He was wearing a shabby, bland outfit that was covered with numerous patches. He was sitting against a large oak tree, and there was no sign of a musical instrument in the immediate proximity. In fact, the only object the minstrel appeared to have with him was a very large block of cheddar cheese.

"Excuse me." started Marcus. "I am trying to find my way to the Castle of Cheswick. Would you happen to know the way?"

"It is not a real castle, you know. It is a cheese factory." responded the minstrel.

"Yes. I am aware of its function." answered Marcus. "What I am not aware of is its location."

"Why do you want to go there?" inquired the minstrel.

"Is this a test?" asked Marcus. He was suddenly suspicious. In his experience, only trolls asked random travelers this many questions; and questions from trolls rarely ended well.

"No." answered the minstrel with a long sigh. "I am just curious. You see, I am the cheese minstrel. I am commissioned to tell stories about cheese. So I am naturally interested in anything to do with Cheswick Castle."

"A cheese minstrel?" asked Marcus in disbelief. "I have never heard of such a thing."

"We are not exactly the most highly regarded minstrels in our community." answered the minstrel. "We do not get to sing about knights, dragons, or epic tales. It is actually rather demoralizing. But that is what I get for partying during minstrel school. My mother always told me to take it more seriously, but I didn't listen. Now, I am working on a song about cheddar." the minstrel waved weakly in the direction of his large block of cheese.

"I see. I am sorry to hear that. But I really am in a hurry. Can you tell me where Castle Cheswick is?" Marcus prompted.

"Are you on an important mission?" asked the minstrel with a gleam in his eye. "There are not many important missions that involve cheese factories."

"Yes. I am." answered Marcus. "I believe that there is something foul going on at Castle Cheswick."

"Oh…" The Minstrel sounded disappointed. "That is probably just one of their new varieties. Some cheeses smell stronger than others."

"That is not what I meant. I recently received cursed cheese." explained Marcus.

"Cursed cheese?" asked the minstrel. "I am not familiar with that type."

"It is not a type of cheese. It is a cheese with a curse on it." replied Marcus. "A very particular type of curse that targets wizards with knowledge of computational spells. It is a subtle curse too -- easy to hide in something strong like bleu cheese."

"Then how did you find it?" asked the minstrel. From the minstrel's body language, it was obvious that this conversation was the most exciting thing to have happened to him in the last five years.

"I validate all my food, of course." stated Marcus. "Every good wizard should. Although, admittedly, too many get lazy."

"Validate?" asked the minstrel. "Do you mean you sniff it to make sure that it is not spoiled?"

"I am a little more thorough than that!" proclaimed Marcus. "It can be a big problem if the food that you get is not what you are expecting. You need to validate it. I have caught many problems in the past with my groceries, including:
- the wrong type of food (apple butter instead of peanut butter),
- food containers that are empty,
- expired food, and
- now, cursed food.
But this is the first time that I have seen a spell this well designed."

"So these problems were all intentional?" asked the minstrel in awe. "You must be very important to have so many people trying to mess with your food."

Marcus would assume that the topic of validating one's food would be standard knowledge for someone who spent his life sing epic songs about cheese. But the minstrel seemed to be hearing all of this for the first time.

"Of course it is not all intentional." answered Marcus. "The problems fall into a few categories. There are certainly cases where people do send malicious food, such as cheese with a hidden curse. But there are also other cases, such as getting the wrong food due to a miscommunication or an error on the grocery store's part."

"I remember this one time where I asked for a delivery of twenty loaves of rye bread. They gave me thirty loaves of whole-wheat. What a mix up!" Marcus chuckled.

The minstrel looked confused. "You just said that someone had cursed your cheese and now you are laughing about the wrong type of bread? That sounds like two different problems to me."

Marcus shrugged. "Two different problems, but the same point: you have to validate you input."

"And there is also the related problem of validating the inputs to recipes that have subcomponents." added Marcus. "Even though you have control of the inputs does not mean that you do not have to check them. For example, you always need to check that you made the pizza dough correctly before making the full pizza. I once had an apprentice that did not check her dough, and the entire pizza fell apart in the oven. It took her three hours to clean up the mess."

"And then there was incident with the undercooked pasta." continued Marcus.

"To the north." interrupted the minstrel.

"Huh?" asked Marcus. He had been enjoying explaining the concepts behind food validation.

"Cheswick castle is to the north." explained the minstrel. He paused for a moment. "Can I come with you?" he asked.

"Why on earth would you want to come with me?" asked Marcus. "Didn't I just mention the cursed cheese?"

"You are on an epic adventure to find out who placed a curse on your cheese. It is the opportunity of a lifetime for a cheese minstrel like myself."

"Well... Alright." agreed Marcus. It was not possible to argue with that logic. Where else was a cheese minstrel supposed to go?

The minstrel beamed as he collected his block of cheese and prepared to follow Marcus. Together the two of them set off toward Cheswick Castle.


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

For more practical tips, also see: how Marcus teaches the Importance of Unit Testing Magic Spells, the problems that can arise from forgetting Variable Initialization in Busy Kitchens, or the Importance of Good Comments in Bread Recipes.

No comments:

Post a Comment

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