Wednesday, June 29, 2011

Variable Initialization in Busy Kitchens

Variables store values in computer programs. In some programming languages, variables are given a default value when they are created. In other programming languages, including C and C++, variables must be initialized. Until they are initialized they hold whatever value happened to previously occupy that location in the computer's memory. Forgetting to initialize variables can lead to unexpected and undesirable behavior.

The kitchen was hectic. Five chefs and ten assistants were working in a tiny space, preparing the feast for the king's birthday.

"I need another bowl" shouted the dessert chef.

His assistant quickly scurried over to the nearby dish counter and grabbed a bowl. He placed it at the edge of the chef's table before returning to his task of peeling radishes. The assistant knew that efficiency was key in such hectic situations. When the chef needed something, he needed it now.

"What the heck is this?" bellowed the chef.

The assistant trembled as he looked over. Why was the chef angry?

"This bowl is filled with rotten tomatoes! My goodness, they are covered with blue fuzz!" The chef looked horrified.

The young assistant quickly realized his mistake. In his eagerness to get a bowl for the chef, he had forgotten to make sure that the bowl was correctly initialized. In this case, that meant that the bowl was clean and empty. Since the bowls on the dish counter were almost always clean and empty, the assistant had not even thought to check.

Unfortunately, the chef had made the same mistake. He had dumped the pudding that he was making into the bowl before confirming that it was indeed empty. Now the bowl was filled with an unpleasant mixture of the chocolate pudding and rotten tomatoes.

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

No comments:

Post a Comment

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