So, this week's lab was all about refactoring. "Cool," I'll just clean up my own project. How hard could it be? It's my code, I know it inside and out.
Turns out, I was wrong. It was way harder than I expected.
The Code Crumbles
My first target was my main.py file. I honestly had no idea how long and messy it was. My plan was simple: find some part of the main.py and place it into a new separate function.
This is where the problems started. The moment I tried to lift that block of code, it felt like I was tearing a piece of bread in half. Little bits and pieces just start to crumble everywhere. First, it was variables. I had variables that were only defined back in main and I kept on forgetting to send them as an argument. Secondly, a bunch of other tiny little bugs that popped up. I had to spend most of my time just picking up these "code crumbles" to get the program running again.
More Efficiendy!
After finally refactoring main.py into shape, I moved on to the next task: refactoring def get_git_info(repo_path): function. This function was executing git log 5 times in a row to fetch the required data and this is very inefficient. Instead, I added git_format = "%H%n%d%n%an <%ae>%n%ad" line to ask git log to print in order of commit hash, branch info, user, date. I used new line \n as a delimiter. This is much more efficient way to get the information without repetitive execution.
Humans Repeat Their Mistakes
Refactoring def get_git_info(repo_path): was successful but I realized that now it also reads the __pycache__ folder. So, I decided to work on making the directory exclusion list configurable. The plan was to add a list of folders to ignore in the .toml file, update main.py to read it, and teach file_utils.py to actually skip those folders.
When you implement a new feature, you must test it. I ran the test, and... it didn't work. The __pycache__ folder was still sitting there in the output.
Turns out it was the same mistake that I made from last lab. I placed .repo-code-packager-config.toml in a wrong folder. Once I moved it to the root folder, it passed the test.
Overall
I got to be honest, the whole process just felt boring. I had to read all the codes again, even though I wrote it, there were some places that I need to analyze in order to think of a effective refactoring process. Now that I look into the result, I learned that it is one of the most important procedure when you are building a program. Lesson today, Got to do what it takes for the better result.
Top comments (0)