CodeSOD: Classic WTF: Let Me Sleep On It
We're starting our Thanksgiving break a day early this year. To make up for it, we're dipping back into the archives for a classic WTF. Original
"Perl is a language for getting your job done," is the underlying philosophy of the language. The only right way to write a Perl program is whatever way works. The ultimate flexibility of Perl is a breeding ground for WTFs . That's doubly true when you're new to the language, like Dave once was.
To get Dave started with Perl, his boss paired him up with Alvin, the veteran Perl programmer. He'd been using Perl since version 4, and had a reputation for wielding regexes like a scalpel. After Dave had a few days of ramp up, Alvin started sending him code from their codebase so that Dave could try and understand how their applications worked.
Alvin's style might have used a lot of doSomething(foo) unless condition; constructs , but his coding style was fairly consistent and clear. Dave followed his code pretty well- although one convention really confused him:
#after writing a bunch of stuff to a fileclose(MYHANDLE);sleep(5);
Every close had a matching sleep. Dave couldn't help but ask Alvin, "Um" why is that sleep there? "
Alvin gave Dave the Starea- that alpha-nerd, hairy-eyeball that makes you wonder, "Did I just say something incredibly stupid?" Just before Dave caved in and said "never mind", Alvin replied. "It's to make sure the file handle is really closed before the program continues."
"I don't think close works that way," Dave said.
"I'm pretty sure it does," Alvin said.
"I don't think so," Dave said. He made a copy of the script in question and with what little regex-fu he had learned so far, he replaced all the close/sleep pairs with just a plain old close. He ran the script, and as you might expect, it worked flawlessly and took about 15 seconds fewer to run. "See?" Dave said, "You don't really need those."
"Apparently," Alvin said.
They spent the rest of the afternoon tracking down major places where there were close/sleep pairs. Alvin grudgingly let Dave remove them and check the changes into CVS after testing them to prove it still worked. Dave went home feeling pretty clever. He really was learning this Perl stuff.
Dave slept on it, and he felt pretty good about everything until he got into work the next day. Over night, Alvin had gone back through the code and added in each and every one of those sleeps, and he made them all one second longer. Dave stopped down at Alvin's cube. "Hey, about those sleeps- you put them back?"
Alvin gave Dave the Starea. And he didn't stop until Dave retreated to his cube. Apparently, those sleeps really were important to the application.
[Advertisement] Forget logs. Next time you're struggling to replicate error, crash and performance issues in your apps - Think Raygun! Installs in minutes. Learn more.