Code Refactoring Tips In C#

Rearranging the code provides readability and convenience for the programmer. In this article, we will rearrange a code to optimize it.

In this tutorial, instead of using a large code, we will learn how to edit code with examples. After this lesson, we will do code editing operations on a sample project.

Use Comment

When you look at the code later, you can use comments to remember what a function does or why you use that function.

The point is not to write too many comment lines, nor do you keep large sections of code as comment lines.

Now let’s increase the readability of the code by using comments on a sample code snippet. You will see two separate pieces of code, the old one and the edited version.

static void Main(string[] args)
{
   int sum = func(111 , 121);
   Console.WriteLine(sum);
}
        
public static int func(int a , int b)
{
   return a + b;
}
Refactoring:

static void Main(string[] args)
{
   // 111 + 121
   int sum = func(111 , 121);
   Console.WriteLine(sum);
}

// Gather Function
public static int func(int a , int b)
{
   return a + b;
}

These steps, which seem simple for now, are more functional in complex projects, although they may seem unnecessary because we are using simple functions at the moment, their importance will increase as the project grows.

Use Functions

Too much code in the main program makes it difficult for the programmer to read. It also complicates the programmer’s jobs.

Separating each code into functions according to its purpose increases not only readability, but also your code speed.

static void Main(string[] args)
{
   int ammo = 3;
   int damage = 10;
   int health = 100;

   for (int i = 0; i <= ammo; i++)
   {
      Console.WriteLine("Press X For Shoot");
      string x = Console.ReadLine();

      if (x == "x" || x == "X")
      {
         Console.WriteLine("Your Ammo: " + (ammo - i));
         Console.WriteLine("Damage: " + damage);
         Console.WriteLine("Health: " + (health -= damage));
      }
   }
}

As you can see, we have filled the main function with a single code, now let’s combine all these codes with the function.

class Program
{
   public static int ammo = 3;
   public static int damage = 10;
   public static int health = 100;
        
   public static void Main(string[] args)
   {
      Shoot();
   }

   private static void Shoot()
   {
      for (int i = 0; i <= ammo; i++)
      {
         Console.WriteLine("Press X For Shoot");
         string x = Console.ReadLine();

         if (x == "x" || x == "X")
         {
            Console.WriteLine("Your Ammo: " + (ammo - i));
            Console.WriteLine("Damage: " + damage);
            Console.WriteLine("Health: " + (health -= damage));
         }
      }
   }
}

Use Classes And Interfaces

We can use class and interface files to make the main file even more readable. We also avoid rewriting the code.

For example, even if all 3 functions are created for one job, we can make the code faster and more readable by storing these functions in a separate main class instead of the main file.

Using classes not only improves readability, it prevents codes from mixing with each other, but also enumerates the codes, and most importantly, increases the speed of the project to a great extent.

Renaming In Project

It is very important to choose the names of variables, classes, and functions that we use in the project.

It is important to rename it as step 4 after reviewing the code, now let’s rename it on the sample code snippet.

class Program
{
   public static int x = 1;
   public static bool y = true;
        
   public static void Main(string[] args)
   {
      func();
   }

   public static void func()
   {
      if (Convert.ToBoolean(x) == y)
      {
         Console.WriteLine("True");
      }
   }
}

Let’s rename variable and function names in a more legible way. Next, let’s add the comment lines to the code.

class Program
{
   public static int number = 1;
   public static bool TrueValue = true;
        
   public static void Main(string[] args)
   {
      CheckEqual();
   }

   public static void CheckEqual()
   {
      if (Convert.ToBoolean(number) == TrueValue)
      {
         // if equal write True
         Console.WriteLine("True");
      }
   }
}

Leave a Reply

Your email address will not be published. Required fields are marked *