5 advice for writing clean code in C#

clean code is essential for updating scripts, adding new features to code, etc. So every developer, must learn how to clean the code.

With the help of clean code, you can increase readability, facilitate your collaboration, and make your work easier for future updates.

Of course, every developer is cleaning own code with different methods and styles, but in this article, we will give 5 pieces of advice to help clean the code for you.

Indentation styles

Many developer use 2 common indentation styles these are BSD and K&R styles. Although BSD is usually considered more legible, the number of people who prefer K&R is also high.

// It's K&R
void Greetings(){
  Console.WriteLine('Hello');
}

// It's BSD
void Greetings()
{
  Console.WriteLine('Hi');
}

In this article, we will use the K&R style for other code samples. It’s just a preference, you can select one for your project.

Although, don’t forget to use a just style in your project everybody has to use the same style for the project otherwise code readability can be reduced.

Another situation with curly braces occurs in nested expressions. You will not get any error when you do not use brackets in nested expressions.

// Without brackets
i = 0;
x = 0;

while (i > 3)
   i++;
   while (x < 5)
      Console.WriteLine(x);
      x++;

As seen in the example, the readability decreases as the statement used increases. We recommend that you prefer to use the curly brackets below.

// With brackets
i = 0;
x = 0;

while (i > 3){
   i++;
   while (x < 5){
      Console.WriteLine(x);
      x++;
   }
}

Naming functions, variables, parameters

Naming is essential to understand what do you working on. For example, if you named a variable with the aB style you can understand it’s a variable when you see this format.

As an example, let’s create 2 scripts that perform the same function, let’s name it according to a format in one and randomly in the other.

// Followed a naming format
int Sum(int firstNumber , int secondNumber){
   return firstNumber + secondNumber;
}

// Randomize naming
int sum(int firstnumber , int SecondNumber){
   return firstnumber + SecondNumber;
}

We know that there are many different case systems, you can use kebab-case or snake-case as an example, but do not use the same naming format for different types.

If you are using capital letters for functions and methods, do not give the same naming format for the variable. Do not change the name format that you do in another function.

// True
int Sum(int firstNumber , int secondNumber){
   return firstNumber + secondNumber;
}

float DecimalSum(float firstNumber , float secondNumber){
   return firstNumber + secondNumber;
}

Use commands to help others and yourself

did you notice ever that you don’t understand what your own code is doing? This may be because you haven’t looked at the code for a long time, and this is perfectly normal, while you may have different responsibilities.

However, you shouldn’t spend hours trying to understand the code again, so use comment lines to add some clarity to your code.

While comment lines make your job easier, they also allow others to have a brief knowledge of what you are doing in a collaborative project.

Comment lines are a useful tool that saves your life in every way, you will not experience any performance loss so do not hesitate to use them.

Single Responsibility Principle

Single responsibility principle or SRP most important principle for the clean code. Your methods, classes, functions, or interfaces just have to do a job.

Yes, as a matter of fact, it is that simple, but it is not always that simple to implement. It is not surprising to encounter functions that do a lot of work at once.

The rule that a function has only one task, which is one of the most important features of functional programming, is the main subject here.

Don’t squeeze your code into a single file

The title sums it all up, actually, you don’t need to write all the code in one file. It’s always a better solution to shred your files with classes and interfaces.

You’ve done all the naming, you’ve written functions suitable for SRP, and you’ve added your comment lines, but it’s still not readable and clean enough?

Most likely the problem here is that you are trying to compress everything into the main file. One of the biggest benefits of OOP is breaking things down with classes.

Feel free to use classes in your code to take advantage of this, but that doesn’t mean you create separate classes for each function.

Conclusion

We took a quick look at how clean code benefits and how to implement clean code. However, for more information, I recommend you to read the books written on this subject.

Clean code has very important for a programmer no doubt and if it is fully learned it will take you one step further in your business life.

Leave a Reply

Your email address will not be published. Required fields are marked *