Week 5: Refactoring
mpa-LHutchinson
Posted on October 10, 2024
Introduction
This week I was tasked with adding 3 small changes on my release 0.1 project, all on separate commits. Then, I would use git rebase to squash all the commits into one commit, merging it with the main branch. Compared to other labs I found this one to be simpler and less time consuming since I wasn't adding anything major to my project, and the only thing I had to do was combine them into one commit.
Getting started
To get started, I pulled the most recent changes to the main branch and created a new branch called "refactoring". While working on these changes, I made sure to add a commit after each one, instead of working on all the changes in the same commit.
The changes
My first change was actually to fix a bug I notice in my code. The last lab was to add the TOML file functionality. If I tried to run the code with a TOML file that had the api key as undefined, eg.
api =
The program would error out. I fixed this so now it simply provides a warning message, and continues with whatever api key the user provided as an environment variable instead.
The second change was another bug fix, I noticed that if the user simply runs the program without any extra arguments, eg.
node index.js
Nothing would print out. But it's supposed to print a welcome message. I figured out that since I added proper exit conditions to the code, it would exit before it could ever reach the check. To fix this, I just moved the check to the top of the code instead of the bottom.
The last change I added was a simple variable rename. In both files that are used to run the program, a variable named "modelChange" was defined to keep track of which model the AI would use. Since the user is not always changing the default model, I thought it would provide more clarity if I renamed this to "modelNumber". Every instance of modelChange was renamed to modelNumber.
Rebasing
Once I committed all my changes, it was time for the rebase. I ran the interactive rebase command:
git rebase main -i
And I opened the interactive rebase in my editor. I had to refresh on my unix commands a little (like pressing I to go into insert mode and typing :wq to save) but once I did I made sure the last 2 commits were squashed into the first commit. After saving this rebase, I did an amend commit to rewrite the commit message, indicating that it was a refactor and listing the changes I made. Finally, I merged this one commit into main. Here is the commit:
Conclusion
Overall this lab was a simple introduction to rebasing and squashing several commits into one. Not only did I learn about git rebase, but I was able to freshen up on my unix commands and fix my project. I'm glad I had an easy lab this week since there's a lot to do in other courses and hacktoberfest.
Posted on October 10, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.