Become a Ninja .NET Developer with NDepend
Tags: ndepend, codeanalysis, codereview, cleancode
Published: 2020-08-16 18:28:49

As a developer, you are responsible for maintaining and modifying an existing codebase. Or you are a person who understands the value of good code, and you may either strive to keep your project code in the standard state, by following object-oriented and clean code principals.
As humans, we usually find it difficult to track all codebase. Code reviews, in addition, are time-consuming tasks, and sometimes reviewing others’ codes repeatedly becomes problematic. When you are in these situations you may face questions such as:
* How do I detect code smell?
* Why my code starts to smell bad?
* How do I keep track of a huge codebase and code review?
* How do I know which lines should I refactor in legacy codes?

I recently become familiar with killer .NET tools named NDepend.
NDepend is a static code analysis tool that has many features to measure the quality of the codebase.
One of my favorite features is Code Metrics and in this post, I am going to introduce this exciting feature briefly.

## **What is NDepend Code Metrics?**
NDepend's Code Metrics has a lot of built-rules to analyzes and track your codebase. Code Metrics quickly find issues and gives you a report of code smells or violated rules.

## **Installing**
NDepend is a commercial product, you can buy and download it from [NDepend.com](https://www.ndepend.com). You can also download the trial version and analyze your codebase through that.

In this post, we are going to have a look at the Visual Studio extension version of this tool. So, you can directly download and add it to Visual Studio 2019 from the "Extensions" menu "Manage Extensions" or download it from [https://marketplace.visualstudio.com/](https://marketplace.visualstudio.com/items?itemName=PatrickSmacchia.NDepend) and install.

## **Analyzing**
After download and install the extension on Visual Studio then, we can now have a look at a sample of clean rule violated which detected by Code Metrics, and understand how to fix the issue.
To analyze the source code, we need to add an NDepend project to our solution. 
You can add it from the Extensions menu, and choose "Attach New NDepend Project To …". 
In this step, run the analysis by clicking the Analysis button.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yfw8fp780bv7mwvrt0kl.png)

When the analysis is done, NDepend's dashboard will be open providing you with an overview of the analysis results, which is illustrated in the figure.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/az2jxvtm9ux41y33s121.png)

By clicking on the violated count in the Rules area, the "Queries and Rules Explorer" window will be open and you can see the rules in detail. Working with this is easy and by clicking on rule names and choosing the violated class name, you can go to the code file, see the smell code line, and finally fix it.
Note, each time you fix a violated code, you may need to build your solution and re-run the analysis. 

As illustrated in the figure, code metrics has detected a code smell known as "method with too many parameters".  The method with too many parameters is a common type of code smell in projects which is hard to understand and is hard to use/reuse, every time when we want to call it we have to pass so many arguments. The next step is …

## **How to refactor a method with too many parameters?**
In the "Queries and Rules Edit" window there is a description of the issue and provides you with some short tips about how to fix it. Imagine we have a method for register user, that has different parameters from the user profile to the location:

``` csharp
public void CreateUser(string firstName, string lastName, 
DateTime birthDate, Gender gender, bool isEmployed, 
string city, string state, string street)
{
   // some code
}
```
To reduce the method parameters, we can encapsulate the high cohesive (high related) parameters into a class. For instance, we used Person class to store information about user personal info and Address class to store information about the user location.

``` csharp
public class Person
{
private string _firstName;
private string _lastName;
private DateTime _birthDate;
private Gender _gender;
private bool _isEmployed;
 
public Person(string firstName, string lastName, DateTime birthDate, 
              Gender gender, bool isEmployeed)
{
_firstName = firstName;
_lastName = lastName;
_birthDate = birthDate;
_gender = gender;
_isEmployeed = isEmployeed;
}
 
public string FirstName => _firstName;
public string LastName => _lastName;
public DateTime BirthDate => _birthDate;
public Gender Gender => _gender;
public bool IsEmployed => _isEmployed;
}
 
public class Address
{
private string _city;
private string _state;
private string _street;
 
public Address(sting city, string state, string street)
{
_city = city;
_state = state;
_street = street;
}
 
public string City => _city;
public string State => _state;
public string Street => _street;
}
```

The improved and refactored code is equivalent to the following code:
``` csharp
public void CreateUser(Person person, Address address)
{
    // some code
}

```
There are a lot of build-in rules for improvement. We can active/deactivate rules from analyzing. We can write our own rules or customize the build-in rule as our need. For instance, in the "AvoidMethodsWithTooManyParameters" rule the default parameters count is equal or greater than 7, but according to your needs, you may want to change it to less than 4 parameters.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m79jpb4fkkokuu0blhu5.png)

## **Conclusion**
NDepend's code metrics tool helps you and your team to review your own code before commit and put it up for code review. It also keeps your project in the right direction of clean code by tracking codebase and generating reports of rules violated. 
You will become a clean coder intuitively, by following these rules and continuous refactoring.