Connect with us

Hi, what are you looking for?

Programming

Code Refactoring Tips In C#

Code Refactoring Tips In C#
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");
      }
   }
}

Click to comment

Leave a Reply

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

You May Also Like

Programming

This article is prepared to explain concepts that will be important for programming beginners such as data types, variables, heap and stack, and operators....

Programming

This article was created for those who want to use functional programming in their career or who are learning new programming paradigms. We will...

Programming

Functions are pieces of code that combine the integrity of the code used to perform a task into a single structure. If you are...

Programming

Computers can solve an event in different ways, we call these ways algorithms, each algorithm works differently from the others and has different processing...