articles

Home / DeveloperSection / Articles / 5 Tips to Writing Clean C# Code

5 Tips to Writing Clean C# Code

5 Tips to Writing Clean C# Code

HARIDHA P284 07-Jan-2024

Writing clear, effective code that works well and is simple for others to maintain after you've gone on to other projects is one of the most crucial abilities of a C# programmer. With so many things to take into account while writing C# code—variables, methods, classes, and types, to mention a few—this can appear like an insurmountable undertaking. However, by following these five pointers, you can learn how to write clear, understandable C# code that other team members can read and comprehend.

Make sure your variable names are memorable.

This type of misconduct is among the most prevalent. As.NET developers, we frequently wish to complicate things excessively when, for the most part, it is not essential.

It is best to choose a name for a variable that makes sense, is simple to pronounce, and can be easily remembered.

Bad way:

var yyyymmdstr = DateTime.Now.ToString("YYYY/MM/DD");

Good way:

var currentDate = DateTime.Now.ToString("YYYY/MM/DD");

Although it doesn't look like much, there will be a lot more variables as the project grows and develops. And as everyone knows, we frequently don't know what a certain piece of code performs when we open a project after a lengthy absence.

Inconsistent name variation

It frequently occurs to us that we define variables, but we do it for each one using a different structure or, more specifically, a distinct vocabulary. Allow me to clarify:

It is our constant task to seek out the ultimate simplicity. Instead of using different words, we should always use synonyms. Let's look at an actual case.

Bad way:

getUserInfo();

getClientData();

getCustomerRecord();

Good way:

getUser();

That example makes it clear that we are misusing various synonyms for the same concept. Both the current developer and any future developers who might need to understand the code could become perplexed as a consequence.

Use names that can be searched for

You have undoubtedly made multiple mistakes in remembering where you put things. You start looking for it, but you can't seem to find it since you can't remember what you named the variable or how to name the constant. For the sake of your understanding:

Bad way:

clearBacklog(backlog, 86400000);

Good way:

// Declare constants with a searchable name

var MILLISECONDS_PER_DAY = 60 * 60 * 24 * 1000; //86400000;

clearBacklog(backlog, MILLISECONDS_PER_DAY);

Even though it might not seem like it now, this little procedure will save us countless hours of searching and a great deal of hassles (for both us, the current devs, and prospective future developers). Recall that the more readable the code is, the better.

Avoid repeating yourself.

We frequently make this mistake because we unintentionally repeat words or names. Over time, this poor approach leads to a very dirty code because a lot of things will be duplicated. Let's begin our practice now:

Bad way:

Bike MountainBike = new(){

   bikeBrand = "Trek",

   bikeModel = "MX Mountain",

   bikeColor = "Green"

};

void paintBike(Bike mountainBike, string color){

     mountainBike.bikeColor = color;

}

Good way:

Bike MountainBike = new(){

   brand = "Trek",

   model = "MX Mountain",

   color = "Green"

};

void paintBike(Bike mountainBike, string color){

     mountainBike.color = color;

}

Thus, we can observe that writing and thinking less leads to much cleaner, easier-to-read code—something we will look back on and be grateful we did.

Makes use of variable names that are extremely detailed

Sending functions straight as a parameter to another function is another terrible practice. All this does is heat up our heads more than an AMD FX series processor and create a great deal of confusion.

Bad way:

var cityZipCodeRegex = new Regex(@"/ ^[^,\\] +[,\\\s] + (.+?)\s * (\d{ 5 })?$/").Matches(address);

saveCityZipCode(

  cityZipCodeRegex[0].Value,

  cityZipCodeRegex[1].Value

);

Good way:

var address = "One Infinite Loop, Cupertino 95014";

var cityZipCodeRegex = new Regex(@"/ ^[^,\\] +[,\\\s] + (.+?)\s * (\d{ 5 })?$/").Matches(address);

var city = cityZipCodeRegex[0].Value;

var zipCode = cityZipCodeRegex[1].Value;

saveCityZipCode(city, zipCode);


Updated 08-Jan-2024
Writing is my thing. I enjoy crafting blog posts, articles, and marketing materials that connect with readers. I want to entertain and leave a mark with every piece I create. Teaching English complements my writing work. It helps me understand language better and reach diverse audiences. I love empowering others to communicate confidently.

Leave Comment

Comments

Liked By