DEV Community

ElshadHu
ElshadHu

Posted on

Refactoring Code && Git Rebase

Refactoring Is a Lifesaver

First of all, when we write code, we have to think about making the software work. Then we can can change the messy parts of the code. I refactored the code
in my repo. I squashed all commits into one commit and I pushed to origin via git push origin main. I will talk about my Git journey later in this blog.

Code Readability

I used to think that everybody could understand my code, but it doesn't work like that. I even forget what I wrote. Therefore, it is better to keep code clean and readable. Firstly, I created refactoring branch git checkout -b refactoring in order to avoid touching the working code. I started with main module. I moved config logic from main to config_manager module. Later, I opened renderer module because I had a gut feeling that something is wrong with this module. I suddenly saw that for --help, -h and --version, -v features , I saw writing help and version messages in this module. I created two function showHelp() showVersion() in utils module to handle them properly. I also removed unnecessary comments because for making changes straightforward later. Then, I changed magic numbers to constexpr constants and I changed function names to be more descriptive. Also, I removed unnecessary libraries from modules.
For all the steps above, were distributed into 3 commits and worked step by step on the refacotring branch.

Git Journey

As usual, I created a branch using git checkout -b refactoring and while making changes, I made commits using git commit -m <message>. While doing that, I used git --amend -m <message> for changing the message because I used a poor message in commit. After finishing the code refactoring, by mistake I switched to main branch using git checkout main and i did fast forward merge via git merge --ff-only refactoring. Boom! I forgot to squash, but fortunately, I realized it had not been pushed yet, so I used git reset --hard <olderMainCommit>. Then, I changed to the refactoring branch and used git rebase -i, replacing 2 pick commits with squash to merge them into the first commit. To clarify my refactoring message, I used git commit --amend and rewrote it. I then switched back to the main branch and executed git merge --ff-only refactoring. The fast-forward merge succeeded, and just like that, my messy commit history was transformed into one clean commit. Crisis Ended!

Top comments (2)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.