Skip to content

The 5 Guidelines to Write Cleaner Code in C#

14 de March de 2022
clean code tips in csharp

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!


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.