Discount offer: Get 35% off on all editions with the code 35OFF

5 Bad Practices That Can Make Your C# Code Messy — And How to Avoid Them

5 Bad Practices That Can Make Your C# Code Messy — And How to Avoid Them
May 17, 2022
3 minutes read

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:

Loading code snippet...

Good way:

Loading code snippet...

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:

Use Tuples instead classes

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:

Loading code snippet...

Good way:

Loading code snippet...

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 type

Tuples 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:

Avoid modifications by using private members

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:

Loading code snippet...

Good way:

Loading code snippet...

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:

Use conditional operator instead if-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:

Loading code snippet...

Good way:

Loading code snippet...

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:

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:

Loading code snippet...

Good way:

Loading code snippet...

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:

You May Also Like

Scoped vs Transient vs Singleton Service in C#

Scoped vs Transient vs Singleton Service in C#

Care to join me, fellow coding enthusiast, in an exploration...

Palindrome Programs in C# – Complete Guide

Palindrome Programs in C# – Complete Guide

Dear fellow coder, let’s take a journey together into the in...

Fibonacci Sequence in C#: From Zero to Hero

Fibonacci Sequence in C#: From Zero to Hero

Struggling with the Fibonacci sequence in C#? Need a detaile...

Prime Numbers in C#: A Detailed Guide

Prime Numbers in C#: A Detailed Guide

Now, who doesn’t love a little magic trick? But what if I to...

Leave a reply

Loading comment form...

Comments (1)

iiKuzmychov

В #4 можно написать код вообще без условных операторов, просто приравняв булевые переменные. Я понимаю смысл срвета, но для ситуации из примера он не имеет смысла

Loading reply form...