Find, Forgive and Forget Your Coding Indiscretions With NDepend
Join the DZone community and get the full member experience.Join For Free
microsoft developers hear a lot about tools that will make them write faster, more efficient, more refactored, more unit tested code all the time . people don’t often talk of tools that help you to visualise your project’s growing code smell though – among other reasons, everyone’s inner sense of pride may be a behind this, however unless you or your employer has forked out the $11,879 odd dollars for visual studio ultimate edition you don’t have much to go on without more than the basic reporting on cylcomatic complexity, dependency depth and class coupling reports that come in the pro and premium editions. these don’t really help you pin point any of your architectural problems that well – something where ndepend proves itself to be a diamond in the rough.
lately i have been reviewing a number of projects both at work and at home from afar . i haven’t been involved in their everyday development, but i have been responsible for them. this has meant that to save myself going insane i’ve had to look at ways to keep an eye on these project’s code quality and architecture without reading every line of every commit from my team.
this lead to me to looking heavily into static analysis tools like nitriq and ndepend . for me ndepend was definitely the winner between the two because of both its ease of integration with continuous integration and custom cql language for specifying custom rules to audit my codebase.
a cure for common developer guilt
let’s all admit something right now – although everyone has the best of intentions when writing code for a new project, software development is more art form than process and when time constraints and pressure get in the way we all create our own dirty little secrets . you know what i'm talking about here – the project you are really proud of, but if you met a developer who was as skilled or more so than you, you wouldn’t let them 10 feet near your codebase for fear they catch a bad reflection of your capabilities.
we’ve all got these projects.
maybe you justify them to yourself;
“i really didn’t want to over-engineer such a simple project”
“the public api was so clean before i went and added feature x”
what's more than us all having these projects, most of us have also dealt with the guilt of wishing they had the time or power to do something about it . static analysis tools like ndepend help us solve this problem with minimal effort. they allow us to actually see where we may have made bad architectural decision by helping you to both visualize and quantify our codebase’s not so nice bits.
when you can get all this functionality from only 299 euros per license (you would only need one or two of you team to run ndepend), the value you get back in terms of cost savings from increased maintainability can be considerable when compared to the functionality it offers compared to the ~$11,000 it costs per license for visual studio ultimate.
what is ndepend and why do i care?
ndepend is basically a swiss army knife for getting a view on all sorts of metrics and visuals on your codebase – and i'm not talking about telling you how many lines of code your solution has. i’m talking about showing you diagrams showing class dependency, architecture, logical flow; things that actually let you get a really good feel for how your application is architected without reading through your whole codebase.
my windows phone 7 application intheknow’s dependancy graph from ndepend
installing ndepend is dead simple, they provide all the binaries in a single zip file.
this may strike you as a bit strange considering this is a project designed to conduct black magic voodoo on your codebase in microsoft development land of all places (“where the hell is my pretty installer/msi biatch!?”). but after discussing this with the developers at ndepend and hearing their reasoning it makes perfect sense and i agree that this is an awesome way to go about things…
“why” you ask?
its dead simple: think continuous integration .
i’ve blogged before that i strongly believe that when it comes to using third party tools that “ they belong in source control ”, and having the entire application running without dependencies on the registry or user profile files really helps achieve this.
while having an xcopy approach to installation helps with continuous integration, it’s awesome to know that there is also a ‘dainty’ visual studio installer that will add custom menu and report additions to visual studio 2005, 2008, 2010. this give you the option to have your metrics run every time you build your solution if you wish – something which from my testing only adds a matter of seconds to even a large project.
if you have never used a static analysis tool before you may be wondering “why would i ever need a tool like this?”.
instead of answering this question directly, lets ask some questions and see how ndepend can help.
metrics that are can be good to know about your codebase:
- what methods and types are in my solution that aren’t in use?
- how reusable is my code? (cohesion)
- are there parts of your codebase where you are boxing your variables; maybe in a part of your application where speed is important?
- are you aware of the code coverage your application has with unit tests?
- how extensible is each of your assemblies? (how many virtual, abstract classes does it have and how many interfaces?)
- if you have a third party dependency and wanted to remove it, are you clear on how big a job this would be?
if any of the above are not things you either keep an eye on, or even have answers for at all static analysis tools can help.
ndepend has over 88 different metrics that it will run against your assemblies, and 4 different kinds of graphs. the latter graphs really help you visualize what's going on in your project from a birds eye view.
i’ve taken the liberty of analysing the codebase for my windows phone 7 app intheknow as i thought it might be interesting to see. i have a number of dependencies and some shared code in class libraries that are inherited by parts of my solution.
intheknow’s dependency matrix.
the next graph that i am now in love with is the metrics graph. this allows you to visualise a number of metrics and the part the play in your overall project.
take my method graph below – each box is the size of the each method in lines of code (you can change this metric to something else such as number of dependencies by choosing from the drop down at the top of the report).
the large blue boxes are the methods that are larger than the configured max size for a method (this is an editable rule) – in this case it looks like i have a bit of work to do in these areas of my project to make them easier to manage and maintain moving forward.
the next one that is also pretty cool is the abstractness vs. instability graph.
the y-axis shows how abstract each of your assemblies is. are you able to extend your code without recompiling it? (interfaces, abstract classes and virtual methods?).
the x-axis shows how unstable your assemblies are. stable refers to the public surface area of your assemblies that cannot be easily changed because there are a lot of assemblies that are dependent on it.
an instable assembly has less dependant assemblies above it in the dependency tree. a value between 0 and 1 is given to each axis based on these equations and your assembly is placed on the graph accordingly.
the easiest way to read this chart follows this basic pattern:
- if your assembly is very stable (i.e.. lots of other assemblies depend on it) and it isn’t extensible (has no virtuals or abstract classes or uses interface based dependency injection) then it will be plotted in the bottom left of the chart named zone of pain (note my assembly “intheknow.common.phone” that is shared between my apps background worker and phone application – obviously it isn’t very extensible at all…).
- if your assembly is abstract, extensible, but isn’t depended on by any other assemblies (it's not really being used) then it is plotted higher on the zone of uselessness.
unique rule based scans
one of the features that sets ndepend apart from other tools like nitriq is their unique cql (code query language) that allows you to write and edit your own scan rules for ndepend to run whenever it runs against your code base.
this enables powerful rules to be created to help you get a hold over where maintenance work needs to be done in your project
take my example below that searches for methods in my codebase for parts that need refactoring (are they too big? too complex? too many parameters or overloads?)
// <name>quick summary of methods to refactor</name> warn if count > 0 in select methods /*out of "yourgeneratedcode" */ where // metrics' definitions ( nblinesofcode > 30 or // http://www.ndepend.com/metrics.aspx#nblinesofcode nbilinstructions > 200 or // http://www.ndepend.com/metrics.aspx#nbilinstructions cyclomaticcomplexity > 20 or // http://www.ndepend.com/metrics.aspx#cc ilcyclomaticcomplexity > 50 or // http://www.ndepend.com/metrics.aspx#ilcc ilnestingdepth > 4 or // http://www.ndepend.com/metrics.aspx#ilnestingdepth nbparameters > 5 or // http://www.ndepend.com/metrics.aspx#nbparameters nbvariables > 8 or // http://www.ndepend.com/metrics.aspx#nbvariables nboverloads > 6 ) // http://www.ndepend.com/metrics.aspx#nboverloads
have metrics at all times with continuous integration
i mentioned earlier in the post that ndepend supports integration with your build environment out of the box.
it does this in the simplest form possible by offering a command line version of the tool. this is incredibly handy if you want to either run this as a scheduled task or through a build runner in your continuous integration environment like jenkins, team city or bamboo.
if you are starting out on a new project, then like most operations in a project that are repeatable you should integrate your code metric runner from day 1 to help give you an idea of the flexibility and maintainability of your codebase over time – so plug ndepend in on day dot!
usage for the command line runner:
ndepend.console.exe [path to your ndepend .ndproj file] optional parameters: /viewreport: to view the html report /silent: to disable output and eventual calls to console.read() on console /hideconsole: to hide the console window /concurrent: to parralelize analysis execution /xslforreport xlsfilepath: to provide your own xsl file used to build report. the path specified must be absolute. /indirs dir1 [dir2 ...]: to override input dirs specified in the ndepend project file. the dirs specified must be absolute. if you use the /indirs option, directories specified in the xml ndepend project file are ignored. /outdir dir : to override the output dir specified in the ndepend project file. the dir specified must be absolute. visualndepend.exe won't work on the machine where you used ndepend.console.exe with the option /outdir because visualndepend.exe is not aware of the output dir specified and will try to use the output dir specified in your ndepend project file. /emitvisualndependbinxml: to emit the file visualndepend.bin.xml visualndepend.bin.xml is an xml file that contains the dependencies graph of your application. it can be used for your own purpose. visualndepend.exe uses visualndepend.bin which is a gzipstream compressed version of visualndepend.bin.xml. /help: to display the current help on console
the team from ndepend have created a video to make it easier to walk through setting this up using ndepend in your ci environment .
static analysis tools aren’t for every type of project, and you don’t need to break the bank by going and buying ndepend for all your team members. however, static analysis can give you the birds eye view that you need to get your codebase under control – ndepend gives you this capability in a level of detail that few other products can. having an architect or two on your team using ndepend to keep an eye on the “zone of pain” and “zone of uselessness” will really pay off in maintainability over time and as we all know maintainability is one sure fire way of producing cost savings for your project over time. the additional fact that they have made it easy to plug ndepend into your continuous integration environment with ease makes using this in in your next green field project’s build cycle from day 1 a no brainer.
Opinions expressed by DZone contributors are their own.