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


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Samina Rahman Purba

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.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Samina Rahman Purba


Print Share Comment Cite Upload Translate Updates
APA

Samina Rahman Purba | Sciencx (2022-10-15T03:23:31+00:00) Refactoring My Code. Retrieved from https://www.scien.cx/2022/10/15/refactoring-my-code/

MLA
" » Refactoring My Code." Samina Rahman Purba | Sciencx - Saturday October 15, 2022, https://www.scien.cx/2022/10/15/refactoring-my-code/
HARVARD
Samina Rahman Purba | Sciencx Saturday October 15, 2022 » Refactoring My Code., viewed ,<https://www.scien.cx/2022/10/15/refactoring-my-code/>
VANCOUVER
Samina Rahman Purba | Sciencx - » Refactoring My Code. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/15/refactoring-my-code/
CHICAGO
" » Refactoring My Code." Samina Rahman Purba | Sciencx - Accessed . https://www.scien.cx/2022/10/15/refactoring-my-code/
IEEE
" » Refactoring My Code." Samina Rahman Purba | Sciencx [Online]. Available: https://www.scien.cx/2022/10/15/refactoring-my-code/. [Accessed: ]
rf:citation
» Refactoring My Code | Samina Rahman Purba | Sciencx | https://www.scien.cx/2022/10/15/refactoring-my-code/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.