DEV Community


Posted on

Refactoring code from ssg


This week, I was working on refactoring my own - and collaborated - code.
There were some fields of improvements that I wanted to work on.
Below are some problems that I wanted to improve.

Long main class

Originally I had very long lines of code in my main method.
Except for passing command line arguments, I decided to remove everything else from main method.
I refactored by extracting several functions and called it in main method. ,
I created an Util class called TextUtils and put all the methods that are needed for text file processing into TextUtils class.

Repeated Jobs

Text processing part was written by myself and markdown processing was written by Tracy and Diana, we were having different methods which are somewhat functioning similar.
Writing an html file was in both MDUtils and TextUtils.
We were using different styles, but I extracted the part, that is writing html header and footers except for the body part, and putted it in a utility class called HTMLBuilder.
This class works as a bridge between main and utility classes. I did not want to ruin the basic structures and way of the codes written by other students, I only extracted the part that is repeated.

It processes the input by its filetypes. If it is markdown, it asks MDUtils to create Html file. Then MDUtils borrows writeHtmlHeader and writeHtmlFoot from HTMLBuilder to write pre-defined html file forms.
It works the same for the text files.
By creating HTMLBuilder, I was able to shorten both TextUtils and MDUtils.
And it would be easier to add another feature to support different file types in the future.
These changes can be found here.

Bad Naming

The naming that I wrote in the first was very hard to recognize.
Even I weren't able to recognize right away.
I wanted to improve the variable names so that anyone who would work on this project can understand what is going on.
While I was working on the naming, I found that there were several declarations that could be combined to one lines.

For example,

            String fName = fileName.toString().split("\\.")[0];

            //only getting the file name from the entire file address
            String[] getOnlyName = fName.split("\\\\");
            String name = getOnlyName[getOnlyName.length-1];

Enter fullscreen mode Exit fullscreen mode

The variable fName was to remove ".txt" from the fileName, and getOnlyName was an array containing the file path spilitted by "\".
Finally name was extracting only the last element in the array to get the file name only without .txt.

I was working on the naming, and I found that those codes could be combined into one line instead of using three different variables.

            String name = fileName.toString().split("\\.")[0].split("\\\\")[fileName.toString().split("\\.")[0].split("\\\\").length-1];
Enter fullscreen mode Exit fullscreen mode

After combining, it would use less memory with declaring new variables, and even easier to understand the process.
This was done with some other variable name improvements.

Messy Codes

Last but not least, there were some un-used or unnecessary variables. And there were values that were being used multiple times in the same block of code.
For unnecessary lines and variables, I removed to clean up, and for repeated values, I declared variable and re-used those.
In this process, some comments were revised and updated to clearly explain what is happening.
This process could be find in below links.


When multiple developers work on a same project, it is very important to do some code clean-up regularly because it is important to reduce memory for the program, and even also for the future works.
Even I had to re-set my environment, and download some dependencies from internet because it was little messed up by being worked on several developers, it was a great opportunity to learn how to make the code "better" and the importance of the refactorings.

Discussion (0)