How to write Clean code in C#?

Clean code and why it is important?

Clean code means writing code that's easy to understand, change, and keep up-to-date. When we follow the best ways of writing code and set standards, it becomes clear, works well, and you can rely on it. The significance of clean code lies in its ability to enhance readability, streamline maintenance, and promote reliability for various reasons. Assuming you are someone who writes in C#, let's dive into a few tips for clean code.

Tip 1. Names

Names should

  • Not too short, too long
  • Meaningful
  • Reveal intention
  • Chosen from problem definition

Tip 2. Use Pascalcase/Camelcase

In the below code, you will find PacalCase highlighted in yellow and camelCase in blue.

Naming Convention Example
PascalCase CarModel, ManufacturerName, ModelYear, Price, StartEngine, Accelerate
camelCase speed (used within the Accelerate method), local variable 'acclr'

CarModel

Bonus shortcuts

  1. Ctrl + W - Selects a block of code
  2. Ctrl + Shift + W - Deselects a block of code
  3. Create a method by selecting a block of code by Ctrl + Shift + R

Tip 3. Method Signatures

A method's signature comprises its name, parameters, and potentially its return type.

Example

public void StartEngine()
Method Name Return Type Parameters
StartEngine void None


Tip 4. Method Parameters best practice

Try to use fewer than three parameters for easier readability and avoid complexity in the function.

Tip 5.  Output parameter of the method

Simply avoid! If you wish to, return an object instead.

Tip 6. Variables

Limiting the scope of variables means declaring them within the smallest possible scope where they are needed. This practice reduces the risk of unintended side effects and makes the code easier to understand.

Example. Instead of declaring a variable at the beginning of a method and using it throughout the method, you can declare it closer to where it's actually used. Consider the following C# code:

int total = 0;

public void CalculateTotal(List<int> numbers)
{
    foreach (int num in numbers)
    {
        total += num;
    }

    Console.WriteLine("Total: " + total);
}

In the above code, the variable total is declared at the beginning of the CalculateTotal method, but it's only used within the loop. It would be better to declare the total within the loop to limit its scope

public void ProcessData(List<int> data)
{
    int total = 0; // Declare variable inside the method

    foreach (int num in data)
    {
        total += num;
    }

    Console.WriteLine("Total: " + total);
}

Tip 7. Avoid magic numbers

Avoiding magic numbers involves replacing hard-coded numerical values with named constants or variables to improve code readability.

Tip 8. (Important ! ) Comments best practices

  • Dont write comments; rewrite your code
  • Explain 'whys' and 'hows' and not 'whats

Tip 9. Single responsibility principle (SRP)

Class or method should do only one thing at a time and do it efficiently.

SRP


Recommended Free Ebook
Similar Articles