The 5 Guidelines to Write Cleaner Code in C#

If you follow these 5 steps you will get a much more readable code 🧺🧽

Juan España
ByteHide
5 min readMar 14, 2022

--

Your C# code should be as easy to read and understand as possible, but this can often be difficult because developers have different styles of coding. That’s why these 5 guidelines will help you to write more readable C# code that can be easily understood by everyone on your team or the ones who are going to work on your project in the future.

The number of lines of code that you need to write can also be reduced with well-designed C# classes and functions, since shorter methods and fewer lines usually improve the understandability of your code.

These tips are adapted from Clean Code Javascript. Thanks Ryan McDermott for your great contribution!

As you already know — and for those who don’t know — I’m creating a list exclusively for Writing Clean C# Code. So let’s go see new ways to save headaches.

Cleaner C# Code (2023)

6 stories

Always encapsulate conditionals

Encapsulation is a way that helps isolate implementation details from the behavior exposed to clients of another class.

Bad way:

Good way:

Also, encapsulation allows for greater control over the coupling of the code being written.

Use private/protected members

By not using private/protected members, they are susceptible to being modified (accidentally or unintentionally) and this can cause errors in the code.

We have to keep in mind that we are responsible for the permissions we give in the code. This may seem a bit abstract, but if a string must be read only, specifying that it is read only and not write, is your obligation.

Bad way:

Good way:

By not using set; we prevent it from being accidentally modified in the future.

Learn to use setters and getters

Many times we do not set public, private or protected methods. This prevents us from better controlling the modification of the object’s properties.

Bad way:

Good way:

Also, when inheriting that class, by default, there is the possibility to override that functionality. The possibilities that this allows you are many.

Composition is better than inheritance

Although many do not know whether to use inheritance or composition, let me tell you that it is better to choose to use composition.

At first, many programmers think that inheritance is better but, you always have to ask yourself if composition could somehow model the problem better.

Bad way:

Good way:

To know when it is best to use which one, let’s look at a quick example:

  • Relationship “is-a” (Human-Animal)
  • Relationship “has-a” (User-UserDetails)

Don’t use magic chains

For those who do not know this, magic strings are string values that must be specified in the code. They always generate an impact on the code. In most cases, magic strings end up almost always duplicating themselves and because they cannot be updated automatically, they are susceptible to being a source of errors.

Bad way:

Good way:

In this way, we will prevent these strings from being duplicated and causing errors in case of making any change in any of them.

These examples are simplified, if you want to see them in more depth, I recommend you to go to Cleaner Code in C#.

These ways are a bit more advanced than the ones in the previous article, but they are still simple — but very useful. But from ByteHide, we know that the number of ways to write cleaner code in C# is more infinite than the universe. That’s why I’m creating a list of Cleaner C# Code, and yes, I recommend you check it out.

Cleaner C# Code (2023)

6 stories

If you are interested, don’t forget to follow me and ByteHide so you don’t miss any news!

--

--

Juan España
ByteHide

CEO at ByteHide🔐, passionate about highly scalable technology businesses and .NET content creator 👨‍💻