Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 17, 2022 05:22 pm GMT

5 Bad Practices That Can Make Your C Code Messy-And How to AvoidThem

How do you know if you're following good practices when programming in C#? And how can you avoid the bad practices that can make your code messy and harder to manage? In this article, we'll look at five common bad practices, and explain how to avoid them by using some better alternatives.

Stop check null with if statement

The if statement can have an enormous number of utilities and uses. One of them is to use it to check null. Although this is a correct way, it can be stated a little better:

Bad way:

if (application != null){    if (application.protected != null)    {        return application.protected.shieldLastRun;    }}

Good way:

return application?.protected?.shieldLastRun;

At no point am I saying that you can't use the if statement for null checking, but by having the null conditional (which is what that feature was implemented for), we will get cleaner and easier to read code.

Check out the Microsoft article to learn more: Null coalescing operator

Use Tuples insteadclasses

If you want to return more than one result, the first thing that will probably come to mind is to create a class just for that purpose. This is a correct way to do it but not the best way to do it.

Bad way:

public ApplicationInfo GetInfo(){    var application = new ApplicationInfo    {        Path = "C:/apps/",        Name = "Shield.exe"    };    return application;}

Good way:

public (string Path, string Name) GetApplicationInfo(){    return ("C:/apps/", "Shield.exe");}

For this there are tuples, according to Mahesh Chand in his article Tuples in C#:

"Often, we want to return more than one value from a class method. Prior to the introduction of tuples in.NET, there were three common ways to do so.

  • Out parameters
  • Class or struct types
  • Anonymous types returned through a dynamic return typeTuples solve this problem"

As Mahesh Chand says, tuples solve this problem. So using them is a much better option than creating a new class.

Check out the Microsoft article to learn more: Tuple types in C#

Avoid modifications by using privatemembers

It is not good practice to have the ability to modify members. You have to be careful which of them can be modified or not.

Bad way:

class Laptop{    public string Os{ get; set; } // can be modifiedpublic Laptop(string os)    {        Os= os;    }}var laptop = new Laptop("macOs");Console.WriteLine(Laptop.Os); // Laptop os: macOs

Good way:

class Laptop{    public string Os{ get; } // cannot be modifiedpublic Laptop(string os)    {        OS = os;    }}var laptop = new Laptop("macOs");Console.WriteLine(Laptop.Os); // Laptop os: macOs

If the member will not be modified, it is better not to use set; to avoid any accidental (or intentional) modification in the future.

Check out the Microsoft article to learn more: Get & Set accessors

Use conditional operator insteadif-else

Often out of habit, we get used to using the classic if-else and that's it. I would not really like to consider this as a bad practice but there is a better way to do it:

Bad way:

if (hasApplication){   shouldProtect = true;}else{   shouldProtect = false;}

Good way:

bool shouldProtect = hasApplication ? true: false;

As we can clearly see, although it is a similar alternative to if-else, by using the ternary conditional operator?:, it is much easier to read, understand and we will get a cleaner code.

Check out the Microsoft article to learn more: Conditional operator

Avoid initials as identifier abbreviations

When thinking about clean code, one of the first occurrences may be to use initials to shorten the code in identifiers. This is a good practice but you have to be very careful with it.

Bad way:

//different identifiers but same abbreviationprivate readonly SecurityManager _sm;private readonly SoftwareManager _sm;

Good way:

//No confusionprivate readonly SecurityManager _securityManager;private readonly SoftwareManager _softwareManager;

If you are going to use abbreviations, please, always think twice to avoid possible confusion in the future. Many times easy does not equal optimal.

Check out the Microsoft article series to learn more: Naming Guidelines


Original Link: https://dev.to/dotnetsafer/5-bad-practices-that-can-make-your-c-code-messy-and-how-to-avoid-them-3p0f

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To