A Basic Print Algorithm

Common snail

In the late 90s, Aaron was employed at a small software company. When his coworker Mark submitted a letter of resignation, Aaron was assigned to maintaining the vast system Mark had implemented for an anonymous worldwide company. The system was built in the latest version of Visual Basic at the time, and connected to an Oracle database. Aaron had never written a single line of VB, but what did that matter? No one else in the company knew a thing about it, either.

Before Mark parted ways with the company, he and Aaron traveled to their customer's headquarters so that Aaron could meet the people he'd be supporting and see the system perform in its live environment. A fellow named Boris met them and gave a demo at his cubicle. At one point, he pressed the Print button to print out a report of certain records. After some serious hourglassing, the system displayed a dialog box asking, Do you want to print page 1?, with Yes and No as options. Boris chose No.

More hourglassing. Do you want to print page 2?

And on it went. Not only did Boris have to choose Yes or No for every page, the time to display each prompt was ridiculous, anywhere from 30 to 90 seconds per page.

By the time they crawled to page 30, Aaron was dying inside and could no longer restrain himself. "Why is it like this?!" he half-asked, half-accused Mark.

"The customer wanted it this way," Mark replied, either unaware or indifferent to his coworker's frustration.

"This is the way we want it," Boris chimed in. "We don’t always want to print every page. Sometimes we just want one page, could be page 73."

"But why not give one prompt where the user can type in the specific pages they want?" Aaron asked.

"Is that possible?" Wide-eyed, Boris turned to Mark. "You told us it wasn't possible!"

"It isn't," Mark said with conviction.

Aaron flushed with embarrassment. He assumed he'd put his foot in his mouth in front of an important customer. "Sorry. You're the expert."

Still, the issue niggled at Aaron long after they returned from the customer's site. Once Mark had packed up his cube and departed for good, Aaron tracked down the report-printing code. He found a loop that started off something like this:


  for k = 1 to SELECT MAX(PAGENO) from REPORTTABLE WHERE REPORTNUMBER = theReportNo  
  
begin
SELECT * from REPORTTABLE WHERE REPORTNUMBER = theReportNo;
...

So, for every single page to be printed, the max page number was re-queried and every single record for the report was retrieved anew, never to be retained in memory.

Aaron didn't have to be a VB genius to realize how much this was killing performance. With a little experimentation, he figured out how to implement a dialog box more like the one he'd described in front of the customer. Boris and the other users were thrilled to receive the "impossible" time-saving fix, and Aaron learned an important lesson about how an Expert's word isn't necessarily gospel.

[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published.