Refactoring codes with Git Rebase
Published: 2023-10-11 19:51:49

[Til-Tool Repo](https://github.com/seog-jun/til-tool)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k4vhqrwxcaq1hjdymjua.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yy1zxaa34jqf8rny263f.png)


![After refactoring index.ts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xn47gtgner0ryzb541i.png)

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

3. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42q816mkf6j5mk60pdz3.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1qw0fqfpr8n7haxl3g0.png)

4. Create new functions to avoid redundant codes
There's a huge change in this [commit](https://github.com/seog-jun/til-tool/commit/f80844f2b34f378cdf27380f8a373f2921aabbbb), 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yloi6smw81lyczt0eomf.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x6e93m1tt14j6hdbammx.png)


And if you see the previous [commits](https://github.com/seog-jun/til-tool/blob/bbff6a4ee74d9f931bb99d2ed20bb98912c499c5/src/index.ts), there were 150 repetitive lines on `index.ts` and I created new functions `removeDir`, `writeFile` and `writeMultipleFiles` on the helper.ts file

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otxue1staz241n2lojiu.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtxa8bay6jpcbgorp6xg.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmxh54gn6ab252hblcyl.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uv44ddlyumuvchrpyoxa.png)