Over the past few days, I spent time refactoring my project and cleaning up different parts of the codebase. It was not just about making the code “work,” but about making it cleaner, easier to maintain, and more predictable. I also practiced using Git features like branches, commits, and interactive rebase to improve my workflow. Here’s how the process went.
What I Focused On
When I started looking at my project, I wanted to make the code safer, cleaner, and easier to maintain. I noticed three main problems:
- Some functions were changing global state (like using
os.chdir()
). - There was duplicate logic in how patterns and tokens were handled.
- The output wasn’t always consistent because of unpredictable ordering.
So my focus was on removing side effects, reducing duplication, and making outputs stable.
How I Fixed My Code
-
In
git_info.py
: I removed the globalos.chdir()
and replaced it withsubprocess.run(..., cwd=repo_path)
. This way, commands run safely without changing the whole program’s state. -
In
file_processor.py
: I cleaned up.gitignore
parsing and simplified pattern matching so the logic was easier to follow. -
In
formatter.py
: I added a helper function to estimate tokens instead of repeating the logic in multiple places, and I sorted directory entries so the output tree is always the same.
Each step made the code a little easier to read and more reliable.
Bugs and Breakages
Yes, I did run into some issues:
- Forgetting to pass
cwd
broke my Git commands. - I accidentally filtered out too many files at one point.
- A few changes caused temporary breakages.
But running tests after each step helped me catch problems quickly.
How My Interactive Rebase Went
Once I finished the refactoring steps, I had a few commits on my refactoring branch. At first, I got a little confused about how to squash them together, but after experimenting and checking carefully, I finally understood how it works.
I used interactive rebase to squash those commits into one clean commit. The process went smoothly once I understand properly. I was careful to test after each step, so there were no conflicts or surprises. After that, I amended the commit message to clearly summarize all the changes, merged the branch back into main, and pushed everything to GitHub. The commit for merging into main branch is here merge Commit.
Final Thoughts
Refactoring wasn’t just about making the code shorter, it was about making it safer, cleaner, and easier to understand. The process taught me how important it is to test after each change, and interactive rebase showed me how to polish my Git history. Overall, it was a really valuable experience.
Top comments (0)