Top 7 clean code tips every developer should know – Part #1

Top 7 clean code tips every developer should know

Writing clean and efficient code is an essential skill for any developer, especially when working with a powerful framework like .NET. Clean code not only improves readability and maintainability but also fosters collaboration within teams and enhances overall software quality. This article dives into key principles and practices to elevate your .NET coding skills.

I’ve come across many experienced developers who write code that’s difficult to understand and not pleasant to work with. I’m sure you’ve encountered similar situations.

This post is designed to help you improve the art of writing code, offering simple and effective topics that are easy to implement in your daily development workflow.

 

1 – Use Classes Instead of Multiple Parameters

In software development, passing multiple parameters to methods can lead to hard-to-maintain and error-prone code. 

C#
				public void CreateOrder(int customerId, string customerName,
                        string productName, int quantity, decimal price)
{
  //...
}
			

A cleaner approach is to bundle related parameters into a class. This not only simplifies method signatures but also improves the readability of the code, making it easier to understand and extend in the future.

C#
				public void CreateOrder(CreateOrderRequest request)
{
  //...
}
			

2 – Choose Clear & Descriptive Property Names

Avoid using abbreviations like hp or addr.

C#
				public class Contact
{
    public string Hp { get; set; }
    public string Addr { get; set; }
}
			

When naming properties in your code, it’s essential to prioritize clarity and understandability. Instead, opt for full, descriptive names such as homePhone and address.

C#
				public class Contact
{
    public string HomePhone { get; set; }
    public string Address { get; set; }
}
			

3 – Avoid Magic Numbers and Strings

Hard-coding numbers/strings directly in your code can make it harder to read, understand, and maintain.

C#
				return price * 0.1m;

//or

if (user.Status == 1)
{
    //...
}
			

Instead, define meaningful constants or enums that make your code cleaner and adaptable to changes.

C#
				private const decimal DiscountRate = 0.1m;

return price * DiscountRate;

if (user.Status == UserStatus.Active)
{
    //...
}
			

4 – Avoid Double Negatives

Using double negatives can be confusing and harder to understand

C#
				if (!user.IsNotSubscribed)
{
  //...
}
			

It’s better to use clear, direct conditions to make the code more readable and maintainable.

C#
				if (user.IsSubscribed)
{
  //...
}

if (!user.IsSubscribed)
{
  //...
}
			

5 – Simplify your code by encapsulating conditionals

Complex conditional statements can make your code hard to read, debug, and maintain.

C#
				if (!order.IsPaid && !order.IsShipped)
{
  //...
}

			

When you find yourself writing lengthy if-else chains or deeply nested conditions, it’s a sign that your code can benefit from encapsulation.

C#
				if (order.CanBeCanceled())
{
  //...
}

public bool CanBeCanceled() => !IsPaid && !IsShipped;
			

6 – Remove Unnecessary Comments and Dead Code

If your code is well-written, you don’t need comments to explain it, if it’s not clear, improve your code instead of adding comments.

C#
				// Check if the order can be canceled
if (order.CanBeCanceled())
{
}
			

Dead code refers to methods, classes, or variables that are no longer used but remain in the codebase. Allowing dead code to accumulate can make the project harder to understand and maintain.

C#
				// Dead code: This method is no longer used
//public void LegacyCancelOrder(int id)
//{
    // Legacy implementation
//}
			

Only used and necessary methods remain in the code, self-explanatory methods that do not need to add comments to explain the logic

Comments should add value, not clutter, use comments only for documentation or very specific cases.

C#
				if (order.CanBeCanceled())
{
}
			

7 – Format Code with Proper Indentation and Whitespaces

The code is functional, but difficult to read and understand.

C#
				public int Multiply(int x,int y){return x*y;}
			

Proper indentation and the use of whitespaces significantly improve the readability of your code, making it easier to understand, maintain, and debug.

It not only helps you as a developer but also ensures that other team members can quickly understand your code.

C#
				public int Multiply(int x, int y)
{
    return x * y;
}
			

Conclusion

I hope you found these tips useful and that they inspire you to enhance the quality of your code. Sometimes, the smallest improvements can make a huge difference in readability and maintainability.

Stay tuned for part two, where I’ll share even more insights! I hope these insights inspire you to keep pushing for cleaner, better code.

Now let’s put it into practice, and keep pushing the boundaries!

See you next time!

Share the Post:
plugins premium WordPress