DEV Community

Minsu Kim
Minsu Kim

Posted on

Code Refactoring

In the week 6, the weekly assignment has focused on clean-up and refactoring Static Site Generator(SSG) project and commit history. The most improvement of the SSG project that I focused on are:

  • Removing duplicated and unnecessary functions
  • Refactoring function
  • Create enum-like-class
  • Rename the variables
  • Remove global variables

Removing duplicated and unnecessary functions

Since SSG project is opensource project that anyone can join and alter the code, there are many duplicated unnecessary functions and codes exist. For example, the feature that specifies all the options in JSON formatted configuration file instead of having passing all the option as command line argument added on my SSG project, the main entry point includes calling one function testJsonFirst whether command line argument include JSON file or not. Since testJsonFirst call another function to generate HTML file, this function is unnecessary. Delete the function and call the function that was placed inside of testJsonFirst reducing code complexity and increasing readability.

Refactoring function

On the previous to develop SSG project, I have focused on refactoring function on my code. The previous contributor helped to operator my SSG project on Linux environment. I have to specify which machine currently operates with my SSG project. This single line of code helps to specify the environment RuntimeInformation.IsOSPlatform(OSPlatform.Linux). However, the code above has been used more than 2 times. Therefore, I refactored code to function.

Create enum-like-class

As mentioned above, my SSG project now supports operating on Linux environment. However, the path escape character is differ from environments. The enum class on C# only supports string-int key-value pair. I have to create enum-like class called Separator with computed property to return correct path character for the different environment. creating enum-like class increase the code maintenance because when the code fix is needed, I simply go to Seperator class to fix the path escape charterer instead of searching all the path escape character inside of the code.

Rename the variables

I have very bad sense of naming the variable which is critical to collaborate other developer. I try to rename the variable for better understanding for the future collaboration.

Remove global variables

As the application becomes larger with more features, the global variable become hard to handle because it is hard to figure out which functions read and write the global variable. So I replace global variable into local variable.

git rebase

The git command rebase helps us to squash the multiple commits into one commit which allows commit history cleaner. And use git commit --amend to create commit message. The git rebase is very useful when I work on same topic with multiple commits and allows me to check commit history easier because multiple commits shows in single commit history instead of multiple commits in multiple commit history.

Discussion (0)