C# code refactoring - the importance of naming convention
Tags: csharp, beginners, dotnet, cleancode
Published: 2023-04-09 07:57:36

[The cover picture shows the important of naming their store so that the visitors know what they are selling.]

---

This article is meant for beginners who just started their programming journey. In this post, I will share the important of naming and naming convention. 

> Code is read much more often than it is written, so plan accordingly. - Raymond Chen, https://devblogs.microsoft.com/oldnewthing/20070406-00/?p=27343

I'm not good at naming even though I have been writing for the past 10 years. But I'm still trying to give the best name for all the variables, class or service to make the reader to understand it easily.

I know it will take time to learn it, learn it while you are free. The more you read, the better you will be. Long story short, here are some examples that I can show you. It might be a few now, I will keep adding them when I can remember them. So, make sure you bookmark this page or the entire series. 😂🤣

## Give an appropriate naming to your variable, functions, class and etc.

When I first write a program (a small assignment from my boss 👨🏻‍🦰), I tend to deliver any features or bug fixes faster so that I could shine among the new recruits (I admit I was a bit aggressive back then 😅). In order to deliver my tasks faster, I used to give a very common name like 'a', 'b', 'c', 'temp', 'var1', or whatever name a lazy person can think of to a variable. Believe me, you won't find any issue with these names while you are writing the code. See the example below of how I named the variables. 
  
```csharp
string temp

string a / string b / string c

public class Helper { ... }

public function Temp() { ... }
```

Yes, I can deliver the feature faster among the new recruits. I worked for a small software house and this software house didn't have a tester. Of course, your boss will ask you to test the feature thoroughly before release. TBH, how good a developer can test the program when he is the developer itself 😆? So, that gave me an opportunity to deliver the software to production directly without thorough testing.

Unfortunately, the delivered feature will not always be bug-free. So, the nightmare comes when the user reported bugs to the feature that I developed months ago. I had to revisit my code again and this is how it looks like. 🤦🏻‍♂️ 

```csharp
public class helper {
   public property string A { get;set; } 
   public property string B { get;set; } 
   
   public string Temp (string input) {
      string tempInput = '';
   }
}
```

TBH, I couldn't understand the usage of the properties, nor the functions at that time. It sounds like karma to me that I have to read what I wrote 😆. 

I learned the lesson, I googled for the best practice of the naming convention. To fix the terrible code that I wrote, I refactored the code by giving an appropriate name to the variables, functions, and classes before I start to fix it. The bugfix took more time than it used to be because I need to include code refactoring to the bugfix. This was the time where I learned how important good naming is.

Microsoft releases a better way to name your variables. This holistic document covers from local variable to global variable, the class naming convention, and more. As a beginner, use your 1 or 2 hours to check it out and you can use this information for the rest of your programming career life. :D

https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines