I Taught Myself How to Implement DRY in a Dream

Photo by carolyn christine on UnsplashIt 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 m…


This content originally appeared on Level Up Coding - Medium and was authored by Troy Moreland

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 starting 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:


I Taught Myself How to Implement DRY in a Dream was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Troy Moreland


Print Share Comment Cite Upload Translate Updates
APA

Troy Moreland | Sciencx (2022-07-19T14:07:48+00:00) I Taught Myself How to Implement DRY in a Dream. Retrieved from https://www.scien.cx/2022/07/19/i-taught-myself-how-to-implement-dry-in-a-dream/

MLA
" » I Taught Myself How to Implement DRY in a Dream." Troy Moreland | Sciencx - Tuesday July 19, 2022, https://www.scien.cx/2022/07/19/i-taught-myself-how-to-implement-dry-in-a-dream/
HARVARD
Troy Moreland | Sciencx Tuesday July 19, 2022 » I Taught Myself How to Implement DRY in a Dream., viewed ,<https://www.scien.cx/2022/07/19/i-taught-myself-how-to-implement-dry-in-a-dream/>
VANCOUVER
Troy Moreland | Sciencx - » I Taught Myself How to Implement DRY in a Dream. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/19/i-taught-myself-how-to-implement-dry-in-a-dream/
CHICAGO
" » I Taught Myself How to Implement DRY in a Dream." Troy Moreland | Sciencx - Accessed . https://www.scien.cx/2022/07/19/i-taught-myself-how-to-implement-dry-in-a-dream/
IEEE
" » I Taught Myself How to Implement DRY in a Dream." Troy Moreland | Sciencx [Online]. Available: https://www.scien.cx/2022/07/19/i-taught-myself-how-to-implement-dry-in-a-dream/. [Accessed: ]
rf:citation
» I Taught Myself How to Implement DRY in a Dream | Troy Moreland | Sciencx | https://www.scien.cx/2022/07/19/i-taught-myself-how-to-implement-dry-in-a-dream/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.