Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Armen Hovsepian
Armen Hovsepian

Posted on

     

Become a Ninja .NET Developer with NDepend

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 fromNDepend.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 fromhttps://marketplace.visualstudio.com/ 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

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

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:

publicvoidCreateUser(stringfirstName,stringlastName,DateTimebirthDate,Gendergender,boolisEmployed,stringcity,stringstate,stringstreet){// some code}
Enter fullscreen modeExit fullscreen mode

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.

publicclassPerson{privatestring_firstName;privatestring_lastName;privateDateTime_birthDate;privateGender_gender;privatebool_isEmployed;publicPerson(stringfirstName,stringlastName,DateTimebirthDate,Gendergender,boolisEmployeed){_firstName=firstName;_lastName=lastName;_birthDate=birthDate;_gender=gender;_isEmployeed=isEmployeed;}publicstringFirstName=>_firstName;publicstringLastName=>_lastName;publicDateTimeBirthDate=>_birthDate;publicGenderGender=>_gender;publicboolIsEmployed=>_isEmployed;}publicclassAddress{privatestring_city;privatestring_state;privatestring_street;publicAddress(stingcity,stringstate,stringstreet){_city=city;_state=state;_street=street;}publicstringCity=>_city;publicstringState=>_state;publicstringStreet=>_street;}
Enter fullscreen modeExit fullscreen mode

The improved and refactored code is equivalent to the following code:

publicvoidCreateUser(Personperson,Addressaddress){// some code}
Enter fullscreen modeExit fullscreen mode

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

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.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm enthusiastic about web development.
  • Location
    Yerevan, Armenia
  • Education
    Computer Software Engineering
  • Work
    .NET Core Back-End Developer
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp