Stop repeating code across functions by using wrappers.

Troy Moreland
Level Up Coding
Published in
3 min readJul 17, 2022

--

Photo by carolyn christine on Unsplash

It isn’t uncommon for me to dream about coding. I frequently wake up with fresh ideas about something I struggled with the day prior. This morning I woke from a similar dream; however, the epiphany it sparked was much greater than usual.

Before the Epiphany

Over time, I have started writing functions in my own standard way. A typical function would look something like this:

This example is only to give you an idea of the structure. Now, I rinse and repeat this as I create additional functions like getRecords, addRecord, updateRecord, deleteRecord, etc.

Just using my basic example, I end up repeating over 90% of my code inside these functions. Every function has a try/catch block. Every function has multiple console calls. I knew this wasn’t ideal. I just didn’t know how to improve it.

To make matters worse, I’m prone to changing my standard. It might be something as silly as changing catch (error) to catch (e). Then my OCD kicks in and, next thing you know, I’ve spent an hour just going through and making this change all over the place.

After the Epiphany

I am always learning new concepts and recently started putting a lot of effort into understanding best practices such as SOLID principles and design patterns in general. I think working through examples of these concepts is what lit the spark for my epiphany.

Let’s get to it…

First off, I thought about the fact that you can pass a function as a parameter of another function. So I wanted to see if I could standardize my try/catch block method.

Now, any time I want to “wrap” a function inside my standard try/catch block I simply pass the function into my tryWrapper function. The main benefits of this approach are:

  • I’m no longer repeating the try/catch code in every function
  • I can alter tryWrapper any time and the change will be reflected everywhere it’s used

Next, I needed to add in my standard for logging messages. The only tricky thing here is that I want the name of the primary function to be displayed in the log message. To accomplish this I created an object that would contain all of my “primary functions”. Then I created a logWrapper in a similar fashion to my tryWrapper.

Conclusion

Wow! This brings a whole new meaning to the phrase, “I can do that in my sleep!”

This is exactly how I dreamed it! So many benefits to this approach:

  • Reduces the amount of code
  • Reduces development time
  • Simplifies troubleshooting
  • Provides an extensible framework (e.g. I could add a tryAsyncWrapper())
  • Implements several SOLID principles

Thanks for your time! I always appreciate any feedback!

Level Up Coding

Thanks for being a part of our community! Before you go:

--

--

Started career as a developer in the Marine Corps in 1991. Founded Identity Automation in 2004. Always learning. Always coding. What's next?