Sunday, August 09, 2009
To get the disclaimer out of the way first: I received a free NDepend Professional license for evaluation purposes, but that in no way influenced my opinion of the software. (Anyone who says otherwise shall be named Mike Arrington).
NDepend, a code analysis tool by Patrick Smacchia, is something I had always read about on popular .NET blogs, but never really paid much attention to. Mostly due to the fact that my projects were all small and noncommercial, so I didn’t think much about using something so detailed as NDepend. To be honest, my projects are still small and noncommercial, but I promised a long time ago that I would try it out and blog about it.

The first thing I noticed was that there’s no installer. I had to extract the ZIP to Program Files and add Start Menu shortcuts myself. That wasn’t a major problem, but it still would have been nice to have an installer. Especially with a commercial application.
Getting started was really easy. You can pick assemblies to analyze and NDepend opens a nicely formatted HTML report with more information than I could probably ever use (which is a good thing). All of that information also appears in the NDepend application itself. The charts are interactive and make it extremely easy to jump to classes, methods, interfaces, etc. I really like the Dependency Graph, because I always worry about which libraries I need to include during deployment. The Dependency Matrix is cool, as well, although a little tough to decipher at first glance. The rotated tree view is also a little difficult to get used to, but obviously necessary.
CQL queries are a major part of NDepend. They work a lot like rules in FxCop and allow you define rules using SQL syntax. The application comes pre-loaded with a ton of CQL queries and also includes a nice editor for writing your own. Aside from using them to enforce rules, you can also write CQL queries to retrieve useful information about your assembly.
I couldn’t come up with a suitable custom CQL query for my small test project, but I’ve looked at a lot of the pre-loaded queries and can definitely see where they would come in handy on larger projects. If you already know SQL, then you’ll be able to write CQL in no time.
The bottom line: If you’re working on a fairly large and complex project, or want to use something other than FxCop to enforce coding rules, then you should definitely give NDepend a shot. My only complaint is that there’s no installer.
UPDATE (8/10/2009):
I spoke further about NDepend’s installer-less distribution with Patrick Smacchia, who had this to say:
Concerning the installer we prefer it this way, just to show that we are not gonna tweak the registry in any way; a developer should be able to unzip a file + create its shortcuts very quickly.
He’s also posted a very nice explanation on his blog. Everything he says is completely valid and I’d like to point out that I don’t view the lack of an installer as a deal breaker in any way, shape, or form. It’s a convenience that would be nice, but not necessary. It’s safe to assume that developers know how to unzip a program and create shortcuts.
The reason I stated it was a “complaint” was because I only use one development machine and don’t have a need for portability in my programming tools, so I enjoy allowing an installer to set everything up. That’s obviously not the case for everyone and most likely just shows my laziness.
Monday, April 06, 2009
For my current project, I've implemented a Shared AssemblyInfo.cs file, mostly because every project is from the same company and share some common version numbers. In one of my console applications, I needed to construct a header using the Assembly Title and Copyright information from my SharedAssemblyInfo.cs file. To achieve this, I added the following to the end of SharedAssemblyInfo.cs:
namespace MyNamespace {
public static class AssemblyInfo {
public static string Title {
get {
var attr = GetAssemblyAttribute<AssemblyTitleAttribute>();
if (attr != null)
return attr.Title;
return string.Empty;
}
}
public static string Company {
get {
var attr = GetAssemblyAttribute<AssemblyCompanyAttribute>();
if (attr != null)
return attr.Company;
return string.Empty;
}
}
public static string Copyright {
get {
var attr = GetAssemblyAttribute<AssemblyCopyrightAttribute>();
if (attr != null)
return attr.Copyright;
return string.Empty;
}
}
private static T GetAssemblyAttribute<T>() where T : Attribute {
object[] attributes = Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(T), true);
if (attributes == null || attributes.Length == 0) return null;
return (T)attributes[0];
}
}
}
Now, I can use it like this:
Console.WriteLine(AssemblyInfo.Title);
Console.WriteLine(AssemblyInfo.Copyright);
The best part is that since SharedAssemblyInfo.cs is linked to each of my projects, I can use the AssemblyInfo class wherever I want!
Also, the class is reading attributes from the assembly that's currently executing, which means that if you call AssemblyInfo.Title in Project 1, it will return the value of Project 1's AssemblyTitleAttribute, while Project 2 will return its own value. If you have defined the assembly title in a linked file, however, both Project 1 and Project 2 will return that value.
Sunday, April 05, 2009
Here's an extension method that packs a C# BitArray to a byte array:
public static byte[] ToByteArray(this BitArray bits) {
int numBytes = bits.Count / 8;
if (bits.Count % 8 != 0) numBytes++;
byte[] bytes = new byte[numBytes];
int byteIndex = 0, bitIndex = 0;
for (int i = 0; i < bits.Count; i++) {
if (bits[i])
bytes[byteIndex] |= (byte)(1 << (7 - bitIndex));
bitIndex++;
if (bitIndex == 8) {
bitIndex = 0;
byteIndex++;
}
}
return bytes;
}
Special thanks to the good people at StackOverflow! Enjoy!
Friday, March 20, 2009
I decided to use DocBook to generate documentation for SalesWeb, but I was finding it difficult to remember which tags I could use. Since DocBook is XML-based, it has a schema that can be plugged into Visual Studio 2005/2008 to enable intellisense.
I’ve put together a tiny ZIP file that has everything you need to get started:
DocBook 4.5 Intellisense for Visual Studio
It contains a VS catalog file and the DocBook 4.5 schemas (which I hope are redistributable… if not, let me know and I’ll remove them). Simply extract the zip file to one of the following directories, depending on your version of Visual Studio:
Visual Studio 2005 (8.0)
C:\Program Files\Visual Studio 8\Xml\Schemas
Visual Studio 2008 (9.0)
C:\Program Files\Visual Studio 9.0\Xml\Schemas
DocBook intellisense will be enabled for files with the .docbook extension. If your solution contains DocBook files, you can simply right-click and choose “Open With…” then select “XML Editor”. In my case, however, my Documentation is outside of my solution, so I had to do the following:
- Click Tools –> Options
- Choose the Text Editor –> File Associations page
- Enter “docbook” as the extension
- Choose “XML Editor” as the editor
- Click Add
- Click Ok
Let me know if you run into any problems!
Sunday, May 11, 2008
I don't like messing with DVDs and their cases when I want to watch a movie, so I went and bought a 250 GB Maxtor OneTouch 4 external hard drive. Over the next few days, I plan to rip my DVD collection to it so I can watch them on Windows Media Center via my Xbox 360. As a test, I ripped "Leon The Professional" to my new drive and attempted to play it in WMC.
The movie showed up in my DVD Library just fine.
However, when trying to play it, I got the following message:
DVD Error:
Windows Media Center cannot play this DVD. Please Restart Windows Media Center and insert the DVD again.
To troubleshoot, I attempted to play one of the VOB files in Windows Media Player. However, I was met with this:
Windows Media Player cannot play the DVD because the disc prohibits playback in your region of the world. You must obtain a disc that is intended for your geographic region.
That's odd. It was ripped from an NTSC disk, so I should be able to play it. The solution?
Well, it's not very obvious. First, I removed the drive from my list of watched folders. Then, I shared the external hard drive to the network. I then re-added the drive to WMC, but this time, I referenced it on the network instead of on my local computer, like this:
Viola! "Leon The Professional" plays perfectly now!