Clean Code: Using Metrics in Code Reviews with Visual Studio Plugins

Sheldon Cohen
2 min readAug 21, 2023

--

Photo by Clément Hélardot on Unsplash

In the realm of software development, the quality of code holds paramount importance. Clean code signifies readability, maintainability, and scalability. As a result, when developers engage in code reviews, it’s crucial to employ a well-defined set of metrics and tools. This guide will highlight tools with plugins specifically designed for the Visual Studio, assisting C# developers in achieving and maintaining code cleanliness.

1. Readability

Good code should be easily comprehensible.

  • Metric: Can a newcomer understand the logic quickly?
  • Tool: NDepend offers insights into code readability for .NET applications, including C#. It meshes seamlessly with Visual Studio.
// Less Readable
public void Func(int x, int y, bool cond) {
if (cond)
Console.WriteLine(x);
else
Console.WriteLine(y);
}

// More Readable
public void DisplayBasedOnCondition(int firstValue, int secondValue, bool shouldDisplayFirst) {
if (shouldDisplayFirst)
Console.WriteLine(firstValue);
else
Console.WriteLine(secondValue);
}

2. Complexity

The intricacy of code can provide insights into its understandability and maintainability.

  • Metric: Cyclomatic Complexity.
  • Tool: Visual Studio’s native Code Metrics Tool can adeptly measure the complexity of C# code.

3. Duplication

Avoiding redundancy in code minimizes maintenance overhead and potential bugs.

  • Metric: Lines of duplicated code.
  • Tool: Resharper by JetBrains, a great Visual Studio extension, can spotlight duplicated code sections in C# and offers various other functionalities.

4. Comments to Code Ratio

While commenting is crucial for context, an overabundance might suggest over-complicated code or unnecessary explanations.

  • Metric: Ratio of Comment lines to Code lines.
  • Tool: NDepend can efficiently analyze and report on the comments-to-code ratio.
// Excessive comments
/// <summary>
/// This method adds two numbers.
/// </summary>
public int Add(int a, int b) {
return a + b; // Adds two numbers
}

// Better
public int Add(int firstNumber, int secondNumber) {
return firstNumber + secondNumber;
}

5. Code Coverage

Ensuring substantial code coverage can significantly reduce the chances of bugs going unnoticed.

  • Metric: Percentage of code covered by unit tests.
  • Tool: DotCover by JetBrains is a Visual Studio plugin that facilitates measuring code coverage for C# projects directly within the IDE environment.

6. Dependencies

Keeping dependencies to a minimum result in a more maintainable and resilient codebase.

  • Metric: Dependency count for a specific class or module.
  • Tool: NDepend provides a comprehensive overview of code dependencies, enlightening developers about their potential ramifications.

7. Code Smells

Code smells are subtle indicators hinting that there might be a more profound issue lurking, even if the code functions as intended.

  • Metric: Count of identified code smells.
  • Tool: Resharper in Visual Studio is adept at pinpointing and offering solutions for common code smells.

Conclusion

The quest for pristine code is an ever-evolving journey, and arming oneself with the right tools is pivotal. By incorporating these plugins into the Visual Studio, C# developers are better equipped to adhere to best practices, ensuring a high caliber of their codebase.

--

--

Sheldon Cohen
Sheldon Cohen

Written by Sheldon Cohen

Technology professional with 15+ years of software development

No responses yet