DEV Community

Seog-Jun Hong
Seog-Jun Hong

Posted on

Refactoring codes with Git Rebase

Til-Tool Repo

This week I focused on refactoring my project for the maintainability of my code instead of adding a new feature. I mainly made 4 improvements:

  • Remove global variables on index.ts
  • Separate command-line argument parsing from index.ts and create a new page for parsing argument on argumentParser.ts
  • Improve file and function's name
  • Create new functions to avoid redundant codes
  1. Remove global variables on index.ts!
  2. Separate command-line argument parsing from index.ts and create a new page for parsing argument on argumentParser.ts

Before refactoring Index.ts

In the beginning, I created lots of variables and a command-line interface for parsing arguments, and it looked so messy and I wanted to separate the command-line argument parsing from index.ts file and remove global variables. So, I extracted the command-line interface to the new file argumentParser.ts and this file exports parsing variables to the index.ts file.

argumentParser.ts

After refactoring index.ts

As you see the index.ts file above, the code is more clear and straight-forward.

  1. Improve file and function's name

Also, I found out there are mismatching between filename and function name. In this project, I try to follow my convention, If I create a new function to write a file, the function name would be writeFile and it applies to the filename as well. However, syncReadFile and htmlCreator don't follow the convention and I updated those files

Image description

Image description

  1. Create new functions to avoid redundant codes There's a huge change in this commit, I already knew there are lots of redundant codes for writing files and a directory on index.ts depending on the options as below.

Image description

Image description

And if you see the previous commits, there were 150 repetitive lines on index.ts and I created new functions removeDir, writeFile and writeMultipleFiles on the helper.ts file

Image description

Image description

Image description

After I refactored the codes, there are only 76 lines of code for now. Now I can say it's easy to maintain and find out how the project is working when you see index.ts file. Finally, I made all of commits for refactoring and I used git rebase main -i to rewrite the history of my commits. I wanted to make the 6 commits into only one for simplicity. In the process of rebase, I also used squash to combine multiple commits into one and git commit --amend to update the commit's message, and merged it clearly. Now there's only one commit is showing on my git log.
Image description

Top comments (0)