Saturday, April 23, 2011

Const and Library Books

Const is a modifier in the C++ programming language that marks a variable as constant or un-modifiable. One of the primary uses of const is to simply have variables behave like constants. For example, this can be helpful in making complex equations readable. Another use of const is to prohibit a method from modifying data that is passed in by reference. This prevents intentional or unintentional side effects to that data.


Peter was frustrated. In his first seven months as an apprentice at the library of Alexandria he had never had a patron intentionally deface a scroll. Now it was happening almost every day.


Peter had noticed the first incident while re-shelving a scroll on building donkey-pulled carts. Half-way through the scroll one of the instructions was edited. The phrase "Secure with four nails." had been rewritten as "Secure with three nails (that is good enough)." How could someone do this? Peter knew that the perpetrator probably believed that they were being helpful and improving knowledge. But in reality, they were defacing the library's scrolls with their own opinions.


Peter decided to consult the master librarian.


"It is a sad day." agreed the librarian. "We need to find out who is doing this and ask them to stop."


"Ask them to stop?" cried Peter. "We need to do something more than just 'ask'. How about a lifetime ban from the library?"


The librarian shook his head sadly. "Perhaps." he agreed.


But after three more edited scrolls were found, Peter was too angry to wait. One night after work, he explained the problem to his friend Sarah. She had gone to high school with Peter and was now employed a few blocks over as a wizard's apprentice. She often grumbled dreadfully about the lack of interesting problems for apprentices in her field.


"Oh! Oh! I can help!" she claimed excitedly. She was visibly bouncing in her seat.


"How?" Peter asked suspiciously. He had had mixed luck with help from wizards in the past. In fact, he still remembered the incident that had left him without eyebrows for almost three months.


"I just learned the Const spell of immutability!" she proclaimed happily.


"Const spell of immutability?" asked Peter. This sounded like the type of spell that would end in disaster.


"Yes." answered Sarah. "The spell makes it so that you cannot edit written documents. It is usually used for things that you do not want changed, such as legal documents or poems."


"Poems?"


"Sure. Poets sometimes ask us to cast the const spell on their poems, so that they are forced to stop fiddling with their work. This one poet was stuck tweaking on the same line for 10 years before we finally made the work constant. It actually happens more than you would believe."


"Oh… interesting." was all that Peter could think to say.


"Can I use it on your scrolls? Please? I am so bored. I could really use the practice." pleaded Sarah.


Peter fought back images of the library accidentally being destroyed by a rogue spell. He quickly agreed before he could think better of it.


That night Sarah spent the entire night wandering around the library casting her spell on the scrolls. It was a slow process. Peter sat behind the desk watching nervously. Nothing burned down or exploded, so he considered the night a victory.


As Sarah was finishing, Peter realized that there was a question that he had never asked.


"What happens if someone tries to modify the scroll?" he asked.


"They can't." answered Sarah.


"I know, but what stops them? A voice in the back of their mind? A forcefield? A stabbing pain?"


"Forcefield." answered Sarah. She sounded tired. Then again, she had been casting the same spell over and over for hours.


The next day Peter sat behind the main counter of the library, trying hard not to fall asleep. Then, he heard a man at a nearby desk muttering something loudly. Peter peered over at the man. The man was trying to write something on the scroll, but his hand was stopped inches from the paper. The man looked confused as he struggled to move the pen closer. Finally, the man shouted something at the paper and stormed out of the library. Peter smiled to himself; Sarah's const spell had worked.

2 comments:

  1. No discussions of problems that arise from constness?

    ReplyDelete
  2. Not yet. But I can think of some good follow up stories for that.

    ReplyDelete

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