When not at the office at StartCo Ltd, AJ raises her two kids with her husband, who works at home. A few months ago she was working long hours on a migration to a new codebase, so she made up the lost sleep on the weekends. While her kids were placated with episodes of The Muppet Show on repeat, she would doze off on the couch.
One Saturday afternoon, with the two kids enraptured by “Pigs in Space,” she took her usual nap. A few minutes later, her youngest daughter urgently shook her awake. “Mommy, Mommy! Are you awake, Mommy?”
“I am now,” AJ said. Satisfied, her daughter returned to watching TV and AJ went back to her nap.
The following Monday, AJ performed a code review of some licensed software that corporate bought for a princely sum for the project. While checking the software’s linked libraries for obsolete OS calls, she found the C# equivalent of shaking a sleeping person to see if they’re awake.
namespace Acme.BusinessLogic { using System; using System.IO; public static class FileUtilities { public static void WriteToFile(string fileName, string data) { if (!IsFileLocked(new FileInfo(fileName))) { using (var outfile = new StreamWriter(fileName)) { outfile.Write(data); } } } private static bool IsFileLocked(FileInfo file) { FileStream stream = null; try { stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) return true; } finally { if (stream != null) stream.Close(); } //file is not locked return false; } } }
AJ rewrote the function to use less “exceptional” code, adding the comment “rewritten to avoid toddler logic.”
Her husband and two children greeted her when she returned home. “‘Pigs in Space,’ Mommy!” her youngest shouted. “All right,” AJ replied. She grabbed some coffee to keep awake this time.