Top 10 Tricks for Cleaning Up C# Code

Practical Tips for Clearer, More Maintainable Software

Alex Maher
4 min readFeb 7, 2024

Ah, refactoring. It’s kind of like cleaning your room so you can find stuff easily, but instead of clothes and books, we’re tidying up code.

Here are my personal top 10 tips for polishing up your C# code, lets dive in!

1. Stick to the SOLID Squad

Imagine your code as a team where each player has a specific role. That’s SOLID for you. It keeps your code organized and ready to adapt:

  • Single Responsibility: One class, one job. Keep it focused like a laser.
// Before
public class OrderHandler {
public void CreateOrder() { /*...*/ }
public void SendEmail() { /*...*/ }
}

// After
public class OrderCreator {
public void CreateOrder() { /*...*/ }
}

public class EmailSender {
public void SendEmail() { /*...*/ }
}
  • Open/Closed: Open for extensions but closed for changes. It’s like adding new features without messing with the old ones.
public abstract class Shape {
public abstract double Area();
}

public class Circle : Shape {
public double Radius { get; set; }
public override double Area() => Math.PI * Math.Pow(Radius, 2);
}

public class Square : Shape {
public double…

--

--

Alex Maher

.NET C# dev with 10+ yrs exp, self-taught & passionate web developer. Sharing tips & experiences in C# and web dev.