This week in my Open Source Development (OSD) course, I focused on refactoring my code to improve its quality and make it easier to maintain. It was something I’d been meaning to do for a while, as my code was starting to feel disorganized. Ironically, the application I’m working on is designed to refactor code, but I hadn’t properly refactored my own.
Everything I had written so far was in a single file, which made it difficult to follow. I realized that anyone else looking at the code would probably find it overwhelming, so I needed to break it down into something more manageable.
Running My Refactor Tool on My Own Code
I began by using my own refactoring tool on the codebase. It provided some useful suggestions, like renaming a few functions to make their purpose clearer and replacing some global variables with local ones. These were small but important changes that helped clean up the code and make it more readable.
Restructuring the Project
Next, I created a basic file structure to organize the code better. I split the code into files responsible for different tasks, like managing the ASCII banner, handling file utilities, configuring options, and running the refactor from Gemini. After committing these changes to track my progress, I gradually moved each function into its respective file based on its role.
Once the code was reorganized, I ran a test to check if everything still worked. Fortunately, it did. However, I noticed that using the refactor tool had removed some comments that, even though the code was self-explanatory, I would have preferred to keep. So I re-added those comments and made a few small adjustments to variable names as needed.
Fixing an Missing Feature
Later on, I realized I had forgotten to include the token usage feature from my original code. I quickly added it back, ensuring that all the functionality was still intact. After several commits, the refactored code was complete and working as expected.
Cleaning Up the Commits
By this point, I had accumulated several commits, all related to refactoring. To keep the commit history clean, I used git rebase to squash them into a single commit. I then amended the commit message to make it more descriptive, merged it to the main branch, and pushed it to the repository.
Final Thoughts
Overall, this refactoring process was a good learning experience. There were a few small issues along the way, but I was able to address them without much trouble. I also gained a better understanding of how git rebase works and how it can be used to clean up commit histories, which will be useful in future projects.
Top comments (0)