Over a half of a year ago, the author of NDepend has asked me to try out his tool. I’ve heard so much good about it, but unfortunately I hadn’t really had time back then. But he’s so cool that I’ve been given another opportunity, after 2017 version has been released. I’ve decided to test it on my old project – a shmup game which I was doing for ‚Daj Się Poznać 2016’. I’ll get back to this contest in next month, because the next edition has just started, and I’m taking part again.
We can use NDepend as Visual Studio add-on, but I’m gonna use a native app called VisualNDepend. To get started we simply need to select assemblies to analyze. After a bit of research in project folder, I’ve selected CSharp assembliy, since I don’t have any game .dlls. It automatically detected UnityEngine references and put it into Third party window. That’s it, we’re good to go.
We can click analyze now.
The Dashboard window is extremely useful if we’re working with NDepend for longer time. Baseline could show us the direction where is our porject going. We can notice whether the code complexity and the technological debt are growing. I’ve seen it on my colleague presentation and hopefully I’ll be able to analyze that in upcoming project.
I was a bit surprised with Code Metrics greenish colour. The project I’m analyzing was written after work, often during nights. I didn’t really had time to care about clean code, but it looks like I kept my methods complexity good enough. We can switch Metrics Level between Method/Field/Type/Namespace/Assembly. The size has around 10 options, but it’s worth to mention we can set it to ‚lines of code covered’. I’m pretty sure I’m gonna use it next time when I’ll be writing an app.
Maybe we do have some dependency graphs inside Visual Studio, but it’s always good to have a few more of them. I still remember WROC# 2016 conference and these words:
Coupling will kill you faster than anything.
When I’ve looked at the Dependency Matrix, I’ve seen a little problem. I’m using UnityEngine.UI elements on MenuController and MapController. I should avoid that mistake in my next project. On the screenshot below we can also see ‚Context-Sensitive Help’. Those popups are really helpful, so I didn’t turn them off. Not yet 😉
I love Dependency Graph. When I was writing this post, I’ve realised that AddHP function is using HandleDamage and I had to check out why… Noted down another design flaw – why class HandleDamage has a field HealthPoints? Shouldn’t it be located inside PlayerData (or something like this)? I feel this tab will be the one of most used windows in my next game.
Besides all that amazing stuff we’ve already seen, I believe we got to the core of NDepend. I’ve always wanted a tool that will check if the code that I’m writing isn’t against some standard rules. For example: a method can’t have more than 20 lines of code. NDepend has a lot of build-in Quality Rules and Gates, which are written in CQLinq Syntax. We can easily modify the existing ones or add a new ones.
Nevertheless there is a small problem with Unity3D. E.g. Potentially dead methods query shows a lot of stuff inside, because of Unity3D design. To fix this I had to add these few lines at canMethodBeConsideredAsDeadProc function:
It made the debt a bit lower, but I’ll need to work on my own rules for Unity3D, because still a lot of them won’t apply here. E.g. it requires public fields instead of properties (but I might be wrong!). I repeat – it’s Unity3D problem and as long as you’re using it for regular C# apps it should work perfectly.
I’ve also broken one Quality Gate, but I’m not sure if I can do something to fix this. To reduce the damage I’m keeping these few constants in one file. I’ll need to adjust this one later.
Even when we look everywhere for flaws of our design, it’s sometimes good to take a break and see how many good practices we have actually met. It’s nice to relax while watching Matched Rules window.
If you want to read a bit more about NDepend, come back on June or July. I’m gonna throw this tool into my brand new project and write how it helped me. See you soon.