DEV Community

Samina Rahman Purba
Samina Rahman Purba

Posted on

Refactoring My Code

What I Learned This Week

This week I learned how to

  • refactor my existing code to make it easier to read, maintain, and improve modularity
  • use git rebase to rewrite history on a branch
  • use git commit --amend to change a commit message
  • use git merge

The Refactoring

Until now I had worked on Rwar – my static site generator written in Python, without paying much attention to ways I could make the code cleaner and easier to read. This week was all about cleaning up my code. I refactored my code by reducing code duplication, extracting functions, removing global variables, and giving better names to my variables. Previously, I had already worked on splitting my code up into multiple files. To do the refactoring of my code, I first created a branch called refactoring and made all my changes on this branch.

Git Rebase and Squash

Rebase and Squash are two terms that are completely new to me. Rebasing was pretty cool as when I did git rebase main -i, I was able to see the history of my commits on the refactoring branch. It opened up a vim editor where I could see and edit commit messages and use the word squash beside specific commits to squash them together to make it into one single commit.

Image description

Trial and Error

I made some mistakes during the rebase process as I was trying to initially rebase while being on the 'main' branch, whereas I should have been on the 'refactoring' branch. This actually created a bit of a mess and I had to fix my errors. After that, I also faced some merge conflicts while trying to merge the branch which I had to resolve.

I was also not too happy with the commit messages I had written while refactoring. I believe the messages could be more descriptive versus just the generic way I had written ‘refactor: extract function’ or ‘refactor: no global var’. There was even a time I went to grab coffee in the middle of writing my code and forgot what I had committed and sent the same commit message twice. I did have a change to edit this on the vim editor during the interactive rebase. I then googled best practices for commit messages and learned a whole lot. I will be sure to follow these practices in the future.

Top comments (1)

Collapse
 
batunpc profile image
Batuhan Ipci • Edited

git pro