DEV Community

Majd Al Mnayer
Majd Al Mnayer

Posted on

Optimizing OptimizeIt

This week is a nice change of pace. We are tasked with refactoring our projects!

This means renaming badly named variables, abstracting away logic, creating functions out of duplicate code in several files, looking out for any performance-impacting logic, renaming files, rearranging directories, and more.

Refactoring

This required a deep dive into the code, reading every line, and tracing execution because I did not want to make any modification that would impact the current functionality of the program. I just wanted to make it better in any way possible.

At first glance, I could see some duplicate code in my flag handlers. This was an easy fix. I factored out the duplicate code, made a helper function, and called it where applicable.

Afterwards, I noticed that the file structure in my project was a bit chaotic. I then decided to move all helper functions to their own helpers folder. There are two such folders: one is a global helpers folder, and the other is specific to argument handling.

Then, during further inspection, I noticed some code was out of place. For example, I had a function that handles file names in the argument handler file. I moved it into its own file, attempting to separate all code that did not share the same context into their own files.

After completing these three refactors, I decided to use my own tool on itself! That's right, OptimizeIt is going to optimize it! I used OptimizeIt to optimize main.ts, because it was starting to get unmaintainable with lots of different logic existing there—some for handling output, some for processing files, and some for executing it all together.

OptimizeIt then optimized it by separating each chunk of logic into its own function! This greatly enhanced not only readability but modularity as well. I then proceeded to create different files in the helpers folder for each of those functions, and now my main.ts looks super clean, elegant, and the complex logic that I once had is now in its own files.

For my final touches, I addressed certain nitpicks I had in the past, such as changing the token usage short flag to -u instead of -tu, separating all interfaces into their own files, and some renaming, such as renaming Config to TomlConfig.

Finally, I bumped the version up from 0.1.1 to 0.1.3, because at some point I forgot to bump the version up to 0.1.2, even though I had a 0.1.2 release on my repo.

Once the refactoring was complete, I then squashed all of the commits into one, amended it, and merged into main!

Overall, more work was done than I anticipated. You can see all of the changes in this commit. However, I now feel much more confident about the state of OptimizeIt, as it's looking very modular, easily maintainable, and super readable!

Top comments (0)