DEV Community

Devansh Shah
Devansh Shah

Posted on

Refactoring Code

This week, I started refactoring my code for FireLinker a cli tool that finds broken links in a given document. I went into this thinking my code was pretty clean and did not need much refactoring and there are a lot of thing I have done right with the code base such as having a single class for links with multiple attributes and functions to reduce code repetition and my code is fairly DRY (Don't Repeat Yourself). But as I analyzed the code I found a code that was not clean for maintainability such as having global variables and some unused variables.

So what did I refactor?

The things I refactored in my code was I got rid 2 global variables
I created a start function that took all the global functionality of yargs and my base logic to start document processing. While, all these where small changes I also added the "use strict"; directive to make sure all future code is done in strict mode.

What did I learn?

I learned my code is not perfect, I wanted to modularize my code in this refraction but I found there to be a larger problem with my logic of how I kept all my parsed links. which is a global variable and to get rid of the variable I need to change most of my logic. So, I kept the current code and left it for a future iteration. As, my code would have broke if I tried refactoring this. One of the rule of refactoring is making sure it works and me being unsure just left it for the time being. but the whole experience revealed that I have a big change I need to make.

Discussion (0)