How Code metrics helps me, and how will help You.
Hello everyone! It’s been a while since I last posted on my blog. I have some good news about my career, I completed my degree project and now I’m out of college. Hee-haw!🤠
This time I want to share with you a bit about code metrics and how it could be useful for you. First, we need to know what it means code metrics. Let’s get started!
Code metrics values
If you are working on a mid-size o big company with data-intensive applications and some complex software you may know that also implies it’s more difficult to make code reliable and maintainable. “Code metrics are a set of software measures that provide developers better insight into the code they are developing.” You can take advantage by understanding which types and/or methods should be reworked or more thoroughly tested.
There are many extensions and ways to get advantages of Code Metrics and technical debt estimations. I found a good one, that’s called NDepend that will make using code metrics easy as pie.
How does It work?
It runs and analysis over your code base, and provides several metrics and visualizations, as well as allowing you to run queries over your code in the form of LINQ statements, called CQLinq.
In this post, we are not going to focus on how to set up or getting started instead I’ll show you some practical functionality that may be practical for you and your team. These features are:
- Metrics
- Dashboard
- Diagrams
Metrics
NDepend offers you a huge code metrics visualization, all in the mood of making your code reliable and maintainable( the utopia) you can create your formulas to estimate the cost to fix some issues in your apps in terms of person-time.
This cost-to-fix can be seen as a debt the team owns: as long as the issue is not fixed, the debt is not reimbursed, and it has interests in terms of development friction. The code base technical-debt is the sum of all these debt estimations. The technical debt can be seen as the mother of all code metrics. The others code metrics as coupling, complexity can be cover by defined rules.
With NDepend we can see our code metrics through a colored treemap, so we first need to install NDepend and add NDepend project to our solution as you see.
To visualize the colorful treemap follow the image steps:
Each rectangle represents a method that is analyzed and given a color based on the metric that is selected: Level = Methods; Size = Lines Of Codes and the result is what you can see in this image, you can also change this metric and make your own queries and visualize it by this colorful treemap.
The Dashboard
Provides a number of quick-access information to give you a snapshot on the results of your analysis. By default, NDepend comes with more than 150 rules that are run over your code, and has a warning. In a build process, you can actually fail a build if one of these critical rules are broken, although you may want to make edits to them if you’re adding this to an existing code base. To visualize the dashboard results you just have to follow this steps:
- Once you installed NDepend, create a new NDepend project and analyze your solution as the previous pic in the metric sections and then select the View NDepend Dashboard option:
- And you’ll see the following output:
- Resume with the line of codes of our project.
- The technical debt we have
- Issues that NDepend detect in our code by severity. (The same way as rule, see below)
- Method complexity
- Rules (NDepend has a default set of rules to determine code metric, you can define your rules as well).
As well it provides useful information about the rule violated and how to fix it (4).
It’s really cool how NDepend offers important information of how to improve our code in different size project like this small one and huge projects. It’s a consistent experience that you can experiment by yourself ;)
Now let’s move on to the next Topic Diagrams…
Diagrams
With NDepend is Visual Studio it is easy to quickly generate and browse any class dependency diagram you need to understand how your code is structured. To generate this diagram you just have to click “Dependency Graph” as shown.
- And you’ll have the following output based on your project structure:
I find this functionality useful when you are working with an existing project and you don’t understand how it is structured that’s fundamental to solving problems and create new functionality without creating spaghetti code.
Conclusion
NDepend is a good tool for getting started into code metrics, offers good visualizations, tips, and really helpful summaries of code analysis that make it easier to create maintainable and reliable code, that is our goal at the end of the day. 😊
Hope this is useful for you, have a good day.😊💚 💙