DEV Community

Cover image for Refactoring Code
Emily
Emily

Posted on

4 1

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay