Last week I shared the first part in a very interesting session that I had the pleasure to do with Patrick over at NDepend. Having readable code and manageable solutions is very important but in some cases small changes that might see, trivial to you, might not be as trivial to someone else.
If you had to keep track of all dependencies in your head you are not going to have anything else on your mind; which is a pretty bad way to distribute your brain! Instead of keeping it all in your head, you can use a tool like NDepend to find out a lot of interesting things about your projects. If you’ve seen the previous posts and videos that I have done on NDepend, you know what kind of power this tool delivers.
The webinars that I have had the pleasure to do with Patrick has been very educating and I hope you like them as well, I would love to get some feedback!
So this week we are going to dig deeper with NDepend and spot dependencies, dependency cycles and spaghetti code! As we look on mscorlib, you can see that in some cases bi-directional dependencies are by design, but in other cases it might have been accidental. Sit back and enjoy the two parts below!
Two weeks ago we looked at Dealing with Code Complexity using NDepend. This was the first episode in the webinar series on Code Quality.
This week I am happy to share that Patrick Smacchia and I have recorded the second episode! This week it’s about dependencies, dependency cycles and spaghetti code. I really enjoyed doing these sessions with Patrick and I really hope you will enjoy it just as much. Let me know what you think of it!
Yet another interesting week has passed with lots of things to discuss. Before we dig into this week’s subject, I just want to take a brief moment to share something interesting that I found (it was recommended by someone I know from IRC). There’s a hosting company called EDIS based in Austria that is now providing a very neat service; Raspberry Pi Colocation! As soon as I saw this I shared it with HackerNews and got a response from EDIS on twitter:
It’s worth just thinking about this for a second, let’s say that 1000 RPi’s could host some websites, instead of 1000 1U rack servers; imagine the power that would be saved (and the space).
Now to this week’s subject!
Last week we discussed how we could join new projects and do that efficiently and I got some great tips & tricks from a lot of you (in different channels). This week we are going to follow in those footsteps and see how we can actually find complex code in our applications. While finding complex code is important for all different programming environments; we’re going to look at this by using C#, Visual Studio 2012 and NDepend.
It’s a little bit different this week..
I recently recorded the first episode of a webinar series with Patrick Smacchia, the founder of NDepend and who better to talk about Code Quality and Code Complexity than with him? So this week, I’m happy to announce that you can watch the first video in this webinar series!
It’s a 30 minute webinar where Patrick and I talk about Code Quality, Code Complexity (CyclomaticComplexity) and other things around this subject.
After watching the video, please tell me and Patrick what you thought about the video and tell me what your tips are to find complexity in your code or projects!
The last three Fridays we’ve looked at how to become more productive, how to increase security in your web applications and how to adapt to a real testing strategy. This week I want to talk about how I get deep into the new projects that I join, fast. The projects that I’m referring to are those that have been developed on before. The customer never really wants to pay for you to spend too much time learning the code base and the actual project, so you’ll have to use tools that will help you do so fast.
What I do when I first get the project on my table
The first thing that I obviously try to do, is get the project running or get all the tests to give me green lights. This assumes of course that there are tests written for the system! Just imagine that you need to join a project that has been developed for hundreds or thousands of hours and there’s no tests and no documentation; this is much more common than you might think. That’s why the following quote is what everyone should be feeling when writing code.
Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.
As soon as I’ve gotten the project running, or the tests are all OK. I can move on to understanding the code base. Normally when you get a project on your hands, there’s something that is needed to be done, let’s say that you need to implement a feature.
The first thing that I try to do, is get an overview of the complexity and what is depending on what. To do this, I’m using NDepend (which I’ve written about in C# Smorgasbord). NDepend will give you great reports and help you find complexity in many forms. Here’s an overview that shows what is connected to what and the size of the boxes indicate the complexity:
When I find something that I think is too complex, I try to identify if there’s a test for that by analyzing the code coverage. If the code coverage is bad, I try to start writing some tests, this will help me get custom to the project. I don’t worry too much if the tests fail; this means that I need to debug the tests which will force me to step into the complex parts of the system and analyze them further.
To get a great overview of my testing, I use the built in (not in VS2010 Express versions, but in VS2012!) MS Test tooling.
Break & Fix the code
When I’ve come as far as starting to write tests for complex parts that are already in the system; I try to refactor small parts, break stuff to understand the business logic. This all helps me get a great idea of what the particular parts do.
In order to help me get an idea of what is covered by tests and not when browsing around the code, I use NCrunch. NCrunch is great, I configure it to add black dots everywhere to indicate statements that are not covered by tests. Then when they are tested, the dots go green and this is updated in real time!
At this time I can normally start looking at the actual task that I need to perform. Depending on how large the project is and how complex it is, the above process might be short or long. But at least for me, it’s a great way to get my head in a new project.
tl;dr
So you’ve got a new project that you need to implement features in, here’s what I recommend:
Analyze the complexity of the code with tools such as NDepend
Write tests for un-covered code to understand the flow of the application
Use NCrunch to annoy yourself with black dots so that you’ll write more tests and get a better knowledge of the code base
Break the code and fix it again — then refactor!
What are you doing to spend less time understanding a system and more time actually fixing the problems?
Since the fall 2011 I have been working on a book called C# Smorgasbord. The book is inspired by all the interesting topics that I’ve touched in this blog.
I’m proud to announce that the book will soon be available for purchase on Amazon and CreateSpace! You will also be able to purchase digital copies on the website for C# Smorgasbord.
It will be available for purchase August, 2012.
The cover
The cover is produced by a very talented art directory, Christoffer Saltelid and looks like this:
3D-view of the book
A little bit about the book
Looking at everything from testing strategies to compilation as a service and how to do really advanced things in runtime; you get a great sense of what you as a developer can do. By taking his personal views and his personal experience, Filip digs into each subject with a personal touch and by having real world problems at hand, we can look at how these problems could be tackled.
No matter if you are an experienced .NET developer, or a beginner, you will most certainly find a lot of interesting things in this book. The book covers important patterns and technologies that any developer would benefit from mastering.
Table of Contents
This is the top-level Table of Contents for C# Smorgasbord:
Introduction to Parallel Extensions
Productivity and Quality with Unit Testing
Is upgrading your code a productive step?
Creating a challenge out of the trivial tasks
Asynchronous programming with async and await
Dynamic programming
Increase readability with anonymous types and methods
Exploring Reflection
Creating things at runtime
Introducing Roslyn
Adapting to Inversion of Control
Are you Mocking me?
If you head over to books.filipekberg.se you can pre-order a digital copy today.
It is truly a great pleasure to finally be able to spread the word about NDepend version 4. For those of you that do not know what NDepend is, you have truly missed out on something great. But not to worry, you can catch up in an instant!
NDepend is a complexity analysis tool that integrates into Visual Studio 2011, 2010 and 2008. It also comes with a standalone component for analyzing your projects.
I have been using NDepend v4 for a while now and I liked it so much that it gets a place in my upcoming book.
What makes NDepend so powerful is that you can customize the analysis to adapt to your own set of restrictions and you do this with what is called Code Query LINQ(CQLinq). This is one of the biggest changes in version 4 and it really takes the tool to another level, prior to version 4 you used another way to customize your queries.
from m in Application.Methods where m.CyclomaticComplexity>15&& m.PercentageComment<10 selectnew{ m, m.CyclomaticComplexity, m.PercentageComment}
I made an analyze of the ASP.NET Web Stack project and this is what the report look like in the stand alone program called Visual NDepend:
In the image above it shows the dependency graph you have a lot of other views that you can use to find complexity in your solution. You can also see a list of the different CQLinq results such as:
Types that are too big
Methods that are too complex
Methods that takes too many parameters
When composing this analysis you also get an HTML Report that looks like this:
It essentially contains the same information as you would get from Visual NDepend, but it is very handy to be able to pass this report on to fellow co-workers.
Another Very powerful thing is that you can integrate NDepend with for instance CruiseControl.NET, which is a build server. This means that every time the build server builds your project, it can also analyze the solution and compare how your complexity increases or decreases.
You can get a 14 day trial, I really suggest you do, you do not want to miss out on this! There will also be much more content regarding NDepend in the book I am currently writing, stay tuned for more information about that.
Filip Ekberg is a Senior Software Engineer working in the country with all the polar bears, author of a self-published C# programming book and overall in love with programming.
Check out my recently published book.C# Smorgasbord covers a vast variety of different technologies, patterns and best practices that any C# developer should master.
All printed copies include access to the e-book bundle (PDF, ePub and Mobi!).