As a coder who often inherits other people work, I am not unfamiliar with the experience of coming across some strange piece of implementation that boggles my, admittedly already somewhat addled, mind. When I come across such a nugget I try to be generous. I try to be forgiving. Maybe the author had some deep insight which I lack; maybe they'd tried and exhausted all the more obvious solutions; maybe they were some abstract genius who could see the woman in the red dress and knew kung-fu... or maybe it was a Friday afternoon and they were drunk. We've all written some ingenious code on a Friday when we were drunk - I can forgive that.

But once in a very rare while I come across a glittering star of insanity, twinkling in the firmament of space. It's hard to offer the benefit of the doubt on these occasions because there can be no doubt. The sheer mind blasting craziness of the snippet I encounter precludes it. There can be no valid reaction to it beyond a dropped jaw and, possibly, a terrified sob.

On these occasions it would be remiss of me not to share. Not because I want to make you better coders but because I want to make you better human beings. It is only through the sheer horror engendered by the dawning knowledge that someone once wrote this and that they are still out there somewhere that we can truly learn how close we stand to the edge of the pit and what tentacled, bat winged monstrosities dwell therein, ready to be vomited forth upon the earth.

No... really... can anyone explain this to me. It's important because I'd like to sleep tonight:-
SQL Code:
  1. CREATE FUNCTION [dbo].[f_GetDuration] (@SystemId tinyint, @IsAnnual bit, @StoredDuration int, @StartDate datetime, @EndDate datetime)
  2. RETURNS int
  3. AS
  4. BEGIN
  5.  
  6.     DECLARE @ActualDuration as int
  7.  
  8.     IF @IsAnnual = 1
  9.     BEGIN
  10.         SET @ActualDuration = 365
  11.     END
  12.     ELSE
  13.     BEGIN
  14.         SET @ActualDuration = @StoredDuration
  15.     END
  16.  
  17.     RETURN @ActualDuration
  18.  
  19. END

I recommend giving it multiple read-throughs. It's only short but it's subtle and layered. I've read it several times and each time I notice another nuance. Some subtlety that I'd missed on previous iteration - presumably because I was blinded by the retina melting glare of the larger insanities. Like a work of classic fiction, it gives, and gives again.