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 onargumentParser.ts
- Improve file and function's name
- Create new functions to avoid redundant codes
- Remove global variables on
index.ts
! - Separate command-line argument parsing from
index.ts
and create a new page for parsing argument onargumentParser.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.
As you see the index.ts
file above, the code is more clear and straight-forward.
- 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
- 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.
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
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.
Top comments (0)