Thursday, November 10, 2011
Names for Ingredients (and Variables)
Thursday, September 22, 2011
Inheritance in Cheeses and Magic Spells: Part 4 of Marcus and the Cursed Cheese
Saturday, September 17, 2011
Classes of Cheese: Part 3 of Marcus and the Cursed Cheese
Sunday, September 11, 2011
Objects, Encapsulation, and Cheese Factories: Part 2 of Marcus and the Cursed Cheese
Thursday, September 1, 2011
Version Control in Magic Spell Development
Thursday, August 25, 2011
Data Validation, Marcus, and the Cheese Minstrel: Part 1 of Marcus and the Cursed Cheese
Thursday, June 16, 2011
Unhappy Magic Flowers and Binary
Binary is a number system where each digit can only take one of two values: zero or one. Binary is used within computers, because it allows the computer to encode information in a series of switches that are either on (1) or off (0). Each digit of binary represents a power of two. The first (right-most) digit represents the 1's place, the second digit represents the 2's place, the third represents the 4's place, and so forth. For example, the binary number 10110 = 1*2^4 + 0*2^3 + 1*2^2 + 1*2^1 + 0*2^0 = 22 in decimal.
The deliveryman paused outside of Marcus's house. He stood transfixed, staring at the flowers. After two minutes, Marcus finally went outside to see what the problem was.
"Your flowers have changed since yesterday." observed the deliveryman. "I am sure that the one on the right was red yesterday. Today it is blue."
"No. Those are the same flowers." responded Marcus. "Some of them are just sulking today. Stupid flowers."
"Sulking? Do flowers sulk?" the delivery man asked.
"These flowers do. They are, of course, magic. If there is not enough rain, they sulk. It is quite aggravating really. It is not as though they are not getting water. I water them every day. Yet, they still insist on sulking when there is no rain. It appears that the water I give them is not good enough for them."
"Huh?" The deliveryman looked most confused. He looked back and forth between Marcus and the flowers.
"They sulk by changing to blue. Roses are supposed to be red. That is why so many poems start out that way. But, they insist on telling me how long it has been since it rained." Marcus gestured angrily at the roses as he spoke.
"They talk to you?" The deliveryman took a step back away from Marcus. He looked as though he was ready to run.
"Of course not. They simply change color." Marcus sighed.
"And, that tells you how long it has been without rain?"
"Well…" started Marcus. "They used to all change color together after three days without rain. A sort of mass protest. But, then they started to organize. They want to let me know exactly how unhappy they are. So, now they count the days."
"But… only two are blue. It has not rained all week." observed the deliveryman.
"Nine days to be exact." corrected Marcus after looking at the flowers. "They use binary."
"Huh?"
"Binary. Red flowers are zero and blue flowers are one." Marcus added.
The deliveryman did not look as though he was following. But, at least he did not look like he was about to run away anymore.
"Binary?" prompted Marcus. "Each flower represents a different digit, and thus a different power of two. The rightmost flower means one (2^0), the one next to it means two (2^1), the one next to that means four (2^2), and so forth. Add up the numbers represented by the blue flowers and you get the total number of days. Right now only first (2^0 = 1) and fourth (2^3 = 8) flowers are blue, so it has been 2^0 + 2^3 = 1 + 8 = 9 days."
The deliveryman looked. Sure enough, the five flowers across Marcus's porch were: Red Blue Red Red Blue (or 01001).
"Why do they use binary?" asked the deliveryman.
"They tried to spell out the numbers on their petals and they got too confused. So they had to settle for each flower being either all red or all blue. It turns out that flowers are not that smart. Binary is a simple enough system for them. If they were smart enough for anything else, do you think they would be complaining to me about the rain? There is nothing I can do!" Marcus shouted the last part directly at the flowers.
"But, how do they work together?" The more absurd the story got, the more interested the deliveryman became. He took a step forward towards the flowers.
"It is really quite simple for them to count in binary. When it rains, they are all happy and turn red. It is like reseting the counter to 00000. I like those days a lot." started Marcus.
"Then, each morning all of the flowers wake up and decide what color they are going to be for the whole day. If it has not rained, they increase the count.
After 1 day they are: Red Red Red Red Blue (00001 = 1)
After 2 days they are: Red Red Red Blue Red (00010 = 2)
After 3 days they are: Red Red Red Blue Blue (00011 = 2 + 1 = 3)
After 4 days they are: Red Red Blue Red Red (00100 = 4)
After 5 days they are: Red Red Blue Red Blue (00101 = 4 + 1 = 5)
And so on.
"You see, each flower looks to its right in order to decide what to do. If its right-hand neighbor changes from blue to red (1 to 0), then the flower flips its own color. I like to think of it as passing along the unhappiness. Basically, if its neighbor is changing from blue to red then the sullenness is rolling over to the current flower. If it is red, then it becomes blue. And if it is blue, it becomes red and passes the sullenness further down the line. This keeps happening until one of the flowers does not change from blue to red."
The deliveryman thought about it. "What if a flower changes from red to blue (0 to 1)?"
"Its neighbor does nothing. Didn't I just say that?" Marcus was impatient. He felt like he was talking to one of his apprentices. "Think about it the way you would counting with numbers 0-9. When you hit 9, you roll the first digit back to 0 and increment the next digit to 1. That next digit stays where it is until its right-hand neighbor switches from 9 to 0 again. You don't count 10, 11, 12, 23. Do you? NO! You wait until the digit to your right rolls over. Only here there are exactly two options 0 and 1, so things roll over more frequently."
"What about the right-most flower?" asked the deliveryman. "How does it know what to do?"
"Ah. THAT ONE IS THE INSTIGATOR! I am sure that he is the one that started it. Every morning there is no rain, he flips. He is the one that starts the process off. Red to blue to red to blue to..."
"And that system always works?" interrupted the deliveryman.
Marcus tore his attention away from the right-most flower. He suddenly wondered how the discussion had gone from a rant about magic flowers to counting in binary. Did the deliveryman not have any other deliveries? For that matter, where was the delivery for Marcus?
"Yes. They already counted out nine days, haven't they?" answered Marcus flippantly.
Then, seeing the look of interest on the deliveryman's face, Marcus returned to his teaching tone. "Imagine that these three flowers were blue." explained Marcus as he pointed to the first three flowers. "That would be 1 + 2 + 4 = 7 days without rain. The next day the first flower (1) switches to red, so the second flower (2) switches to red, so the third flower (4) switches to red, so the fourth flower (8) switches to blue. Then they would show 8 days without rain. The count effectively gets pushed down into the fourth flower."
The deliveryman looked impressed. Marcus could not understand why. The flowers were really very annoying.
"So tomorrow they will be: Red Blue Red Blue Red (01010)?" asked the deliveryman.
"Probably. But, I really hope that it just rains." responded Marcus with a sigh.
Monday, May 16, 2011
The Importance of Unit Testing Magical Spells
Unit tests are small tests designed to exercise specific components of a program, such as individual functions. The goal is to test components in isolation, allowing for: more targeted and thorough testing, quicker detection of errors, and finer grained debugging. Further, ensuring the correctness of the low level building blocks helps to better ensure the correctness of the overall program. Unit tests can also easily detect low level errors that might be masked by other logic in the program.
Marcus did not become a master wizard by being lazy. Unfortunately, it seemed as though the apprentices these days did not appreciate the value of hard work. His newest apprentice, Shelly, was obsessed with moving onto complex spells.
"You have to master the fundamentals first." Marcus insisted.
"But, I already know the fundamentals." Shelly responded petulantly.
Marcus sighed. Kids these days were so difficult. In his day, an apprentice did not argue with his or her master. You listened and were respectful. Why was everyone suddenly so impatient? Marcus blamed the latest pop culture fad: marching bands. Sadly, there was only one way to reinforce the value of correct building blocks.
"Okay." Marcus responded. "Today we will do the spell of Transmogrified Mold, which transforms ordinary house mold into a candy. It is a complex spell that requires six potions and three hours. Are you able to handle that?"
"Yes!" agreed Shelly readily. She was visibly bouncing with excitement.
Marcus smiled as he handed her the directions. "Here you go then. There should be plenty of mold in the kitchen. I believe a certain apprentice has not cleaned there in weeks."
Ignoring the comment, Shelly ran off to the kitchen to begin the spell.
The key to the spell of Transmogrified Mold was correctly making each of the six potions. If any one of the potions is off by even a little, the product is a complete failure. Granted, the result will still look, taste, and smell like chocolate. However, it will cause three hours of intense nausea and powerful hiccups. Together, those turn out to be a most unpleasant combination.
While Shelly buzzed around the kitchen working, Marcus decided to take a nap. No need to remind Shelly to test each potion individually. By this point in her training, the value of testing should be ingrained. Of course, young apprentices always complained that testing their potions slowed them down too much. But, there was really only one way to learn the value of unit testing potions.
Three hours later, Marcus was woken by the sound of loud vomiting. "I believe she is done." he mumbled to himself as he made his way down to the kitchen.
There he found Shelly, with her head in a bucket.
"How did it go?" he asked.
"I don't know what went wrong." moaned Shelly between hiccups.
"Well, let me just see." offered Marcus. He wandered over to the counter and started testing the individual potions.
The first three were correct.
"Ah. Too much vinegar in the fourth potion. Common mistake. My guess is that you did not notice, because there is too much salt in the last potion. Yep. There it is. All very common."
"But, they all looked correct." Shelly protested between powerful hiccups. "And the end result seemed to work out."
"You will be fine in another two hours and fifty three minutes. Until then, I would recommend keeping a bucket close." Marcus advised.
He paused at the door on his way out. He did not look back as he spoke. "And in the future, I would encourage you to thoroughly test each individual component of a spell. You need to make sure the fundamentals are correct before building off of them. Otherwise, you can find unpleasant surprises."
Monday, April 25, 2011
Checkpointing Checkers
Checkpoints are a technique for improving a program's fault tolerance by preserving the program's state. Checkpoints can be used to recover after a program crashes or used to save the state for later analysis. They are literally dumps from the program that are sufficient to reconstruct the program's current state.