Friday, September 9, 2022

The Self-Debugging-Code Design Pattern


Behold! The self-debugging-code design pattern:
try
{
  // Dodgy code here
}
catch (Exception ex)
{
  System.Diagnostics.Process.Start($"https://stackoverflow.com/search?q={System.Uri.EscapeDataString(ex.Message)}")?.WaitForExit();
}

I know, right? 🤯

Just replace the comment in the try block with something stolen from StackOverflow, and we've come full circle.

Obviously, this code is meant to only be used during development, and removed before going in production. Perhaps as a safe-guard (a poka yoke) against this winding up in production, one should put the code inside a conditional method and call it from within the catch block:
[Conditional("DEBUG")]
private static void DebugException(Exception exception)
{
  System.Diagnostics.Process.Start(  [...]
}

There; Now thats responsible developing!