loading...

Indiana Jones Refactoring

leftturns profile image Andy Johnson ・1 min read

I remember as a kid the first time I watched Indiana Jones and the Temple of Doom. Exciting, adventurous, and terrifying at times. When I became a developer a few years ago, I felt the same way. In the films, Indiana is after a specific artifact, preventing bad guys and gals from destroying the world. He travels far and bushwacks through the jungle with his trusty bullwhip to defend himself and get out of danger.

As a new developer, unlike Indiana Jones, I didn't always keep my eyes on the prize. Often I was too timid to refactor methods and functions down to their basic needs. And I assumed the developers before me that took on dependencies were wise and using massive libraries for small benefit was the best choice.

I remember a specific project when I was connecting to a third party API, using their provided Java library. I only needed the authentication method to call their API. My code could handle the HTTP calls, error handling, retry logic, paging, throttling and all the rest. In my case, their method of API authentication was my Ark of the Covenant, or Mystical Stone, or Crystal Skull, for the younger crowd. In other words, I didn't need the jungle, the temple, the jeep, the local to lead me to the temple or anything else. I just needed one core piece. Yet, my code was using that core artifact, and also took on the temple, the jeep, the jungle and so on.

On a bold day I decided that I was tired of maintaining a whole library for one piece, and hacking around that dependency. I decided to grab a machete and bullwhip, and I would bushwack deep into the jungle of that library and extract the artifact I needed. When I stepped in to that breakpoint in my IDE, I knew there was no going back. And after some time finding the authentication classes, I extracted the logic to authenticate the API. It turned out to be one method, that I copied and pasted into my code.

I removed the dependent library and dropped a huge amount of weight on the server hosting it. I learned how to step out of my comfort zone, and gained confidence and felt powerful. I had ripped the heart out of the library, much like in that scary scene in Temple of Doom.

Dependencies aren't bad either. If you use a library for the core of your workflow, using someone else's maintained, secure library is a great way to save time and money. But if you only need one aspect of the library, don't be afraid to dive into their code and see how they did it, so you can do it better. You just have to accept that you're on the hook to update it when they update their API.

Posted on by:

Discussion

markdown guide
 

Hi Andy, thank you very much for the interesting read! I just want to let you know that your article has reached 1st place in Daily - dailynow.co/
It means that thousands of developers from all over the world are exposed to your article every time the open a new tab on Chrome and Firefox. good job :)