23
Brolls
6y

Crazy code.

You know when you come across some code, where you think: “I kinda see what you were going for, but it’s still hella dumb though...” right?

Currently doing some work on an F# backend, and the dev clearly knew enough of the language, but their code makes me question a lot.

The problem is simple: use a third party tool to generate images of each page in a pdf.

Said tool supports:

1. Querying the number of pages
2. Getting all pages as separate images in a single invocation

Can you guess how the dev solved it?

They’ve recursively incremented a page number, called the external tool to grab the image of that page.

“But how does it know when it’s run out of pages?” I hear you ask.

Simple. Catch the inevitable exception, and check against a hardcoded string literal to see whether it says “must be before the end of the document”.

I shit you not.

I nearly had an actual seizure when I was debugging some semi-related code and ended up in this wonderland of fuckery.

The recursion and pattern matching was flawless though, yet the tool’s website clearly states the supported functionality.

The whole thing feels like they tried to do it the right way, but couldn’t be bothered / couldn’t get it right, so they ended up creating this insane bit of madness.

Comments
  • 5
    Perhaps the tool did not support the method in the past....
  • 0
    @seraphimsystems I dunno. It’s pretty crazy code.
  • 0
    @seraphimsystems and it’s fairly recent code too.
  • 1
    I'm not proud to admit it but sounds like something I would do 😆. Like hey the way it should work isn't working for me but I can't think of way, I can however think of this other hacky way that will work let's just give it a try and see if it really will work... aaaand it's in production now woops 🤷
  • 0
    @Mathew-77 😒🤣
Add Comment