DEV Community

Cover image for Refactoring with Git
DerekJxy
DerekJxy

Posted on

Refactoring with Git

This is the 6th Week that I've been being in OSD 600. And this week we have a new work to do -- Lab 5. Different from the previous labs we had, this week we are going to modify our code of SSG program and make it looks better. It's about refactoring our code.

Due to we added new features to our program, the complexity of the code grows with it. We added new features and we forced to create new code paths, functions, variables. which will cause we are start losing control of the code.
Refactoring is a technique for improving the structure and maintainability of our code without altering its behavior.

Procedure

#1. Get the repository to my PC

After reading the instruction of the Lab 5. I cloned my repository to my local machine and then used the command git checkout -b refactor in git to created a new branch named 'refactor'.
And then I use the command code . to run the code in Visual Studio Code.

#2. Go through the code

When my SSG code available in my local machine. I read through my code again. And I found out that there are a bunch of code are similar.
Therefore, I decided to make some new functions to reduce the amount of duplication.

#3. Create Functions

Firstly, I created a function called "mdFileHtmlConversion" to store the code that adding a new feature to my SSG so that all --- in a Markdown file would get converted to an <hr> tag.
Secondly, I found that the way I try to convert a txt file to a html file is very similar the way I covert a md file to a html file.
Therefore, I put then into a new function named "htmlGenerator".
Lastly, I have duplicated logic and code for my program to convert a folder and a single file. In order to make my program with less duplication. I created a new function named "htmlConversion" to store the converting logic and code.

#4. Improve variable naming

Since I updated my code with some new functions, it became more tidy. And my next step was to rename those variables that with a non-sense name. For example, I have a variable named 'fname'. I mean, there are many possibilities for a variable named 'fname', it could be 'first name' or 'file name' or 'french name', etc. So, I changed it to a more specific name 'fileName'. It's way more clear than with the name 'fname'.Also, I changed the variable 'stats' to 'filePath' so that it became easier to understand.

#5. Get rid of global variables

Finally, I removed all the global variables I had in my code. Instead of having global variables, I put those variables to each specific function that I will use them.

#6. Combine my commits

After updating my code, I use this command git rebase master -i to start an interactive rebase, and opening the editor. And then I overwrite the 'pick' keyword to 'squash' so that I can combine all the commits I had into 1 commit. Then I use the command git commit --amend to rename some of my commit descriptions. Last but not least, I merged my 'refactor' branch to my 'master' branch.

My Feelings

I gotta say "Refactoring is interesting!" This is a good way to improve my coding structure. It saved me 53 lines of code after refactoring,tipswhich is almost 1/5 line of code in my SSG program.
Also, my code became easier to work with, easier to understand, and easier to extend! I think I will make more refactor move in the future!

Link to my repo: [Refactoring]

Top comments (0)