DEV Community

Cover image for Refactoring Code
Emily
Emily

Posted on

Refactoring Code

This week, I was working on refactoring my project - Shinny-SSG and experimenting Git Rebase command.

Refactoring code

There were several duplicate logics when processing input in my project. For example, in both Program.cs and SubFolder.cs , I did check the File exist, File extension and calling the FileText method CreateFile and SaveFile.

#Duplicate code
if (File.Exists(inputname) && (inputname.EndsWith(".txt") || inputname.EndsWith(".md")))
 {
  FileText temp = new FileText();
  if (temp.CreateFile(inputname, destination))
  {
   if (temp.SaveFile())
    Console.WriteLine($"File {inputname} is converted sucessfull in {destination} folder");
  }
 }
Enter fullscreen mode Exit fullscreen mode

My solution for this is creating a Generator class. This class is designed to mainly focus on generate File and Subfolder. It has 2 methods are GenerateFile and GenerateFolder. I also deleted the Subfolder class and put all the logic of CreateFolder function to GenerateFolder funtion.
I also fixed issue#10 to using Path class to work with File/Folder paths instead of using string.
Another development I made for my project is removing the Global variables and rename variables.
However , I am still not satisfied with the code design. I still feel it needs more improvement to be cleaner. I will continue to work on it soon.

Discussion (0)