Introduction: Why Clean Code Is Not About Aesthetics, But About Money
Imagine you start a new job, open a C# project, and see a method that is 500 lines long, variables like a, b, temp, and comments written in a mix of Russian and English. Sound familiar? It probably does. Such code works, but maintaining it becomes a nightmare. Clean code in C# is not a perfectionist's whim, but a tool that saves the team hours, nerves, and money.
In this article, we won't lecture you. Instead, we'll break down specific techniques that will help you write C# code that is understandable to a beginner and pleasing to an experienced developer. Let's go!
1. Names Matter: How to Name Variables, Methods, and Classes
The simplest way to make code cleaner is to use meaningful names. C# is case-sensitive, but that's no excuse for int x or string d.
Variables: Say What You Store
Bad:
int d; // what is this? date? distance? money?string n; // name? number? notebook?Good:
int daysUntilDeadline;string userName;The rule is simple: if you can't understand what a variable stores without a comment after a month, the name is bad. Avoid abbreviations like usr or cnt — they save a second when typing but cost minutes when reading.
Methods: Verb + Noun
A method is an action. Name it so it's clear what it does:
CalculateTotalPrice()— calculates the total price;SendEmailNotification()— sends a notification;ValidateUserInput()— validates user input.
Bad example: ProcessData() — too abstract. What does "process" mean? Delete? Transform? Save?
Classes: Singular or Plural Nouns
A class is an entity. User, Order, PaymentProcessor. Avoid suffixes like Manager or Helper unless justified by the architecture. Often, such classes become a "dump" for everything.
2. Methods Should Be Small and Do One Thing
The golden rule of clean code: a method should solve one task and solve it well. If a method takes more than 20–30 lines, it probably needs to be broken down.
Example: Refactoring a Monster
Imagine a method that registers a user:
public void RegisterUser(string name, string email){ // validation if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException(); if (!email.Contains("@")) throw new ArgumentException(); // saving to DB using var db = new AppDbContext(); var user = new User { Name = name, Email = email }; db.Users.Add(user); db.SaveChanges(); // sending email var smtp = new SmtpClient(); smtp.Send("admin@site.com", email, "Welcome", "Hello!");}What's wrong here? Three responsibilities: validation, database work, sending email. If the sending logic changes, you'll have to touch the registration method. Let's break it down:
public void RegisterUser(string name, string email){ ValidateUser(name, email); var user = CreateUser(name, email); SaveUser(user); SendWelcomeEmail(email);}
private void ValidateUser(string name, string email){ if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Name is required", nameof(name)); if (!email.Contains("@")) throw new ArgumentException("Invalid email", nameof(email));}
private User CreateUser(string name, string email){ return new User { Name = name, Email = email };}
private void SaveUser(User user){ using var db = new AppDbContext(); db.Users.Add(user); db.SaveCh