• Frugal Cafe
  • Posts
  • FC76: NDepend, with so much code analysis, code should be great?

FC76: NDepend, with so much code analysis, code should be great?

What kind of code quality you are really spending time on?

NDepend is supposed to offer “an in-depth .NET code quality management experience”. So you would expect itself to have great code quality, right?

Let’s take a look at NDepends.PowerTools source code which is open source. Here is from AppWordsPowerTool:

If you read Frugal Cafe newsletters, you can easily smell issues: the first five ToList calls are not needed, and there are lots of duplicating dictionary accesses in the inner foreach loop.

Now let’s check ExtraWordsFromIdentifier implementation:

string.Split is not efficient, const char array is allocated every time, word list is not needed, the best solution is to use substrings.

Digging deeper:

String list and StringBuilder are not needed. Notice StringBuilder is allocated multiple times here. There is no need to call string.ToCharArray. Goto statements can be easily avoided.

Multiple string.Replace(string, string) calls are expensive. The Pascal casing implementation is slow. Most of such casing can be avoided if you just use StringComparer.OrdinalIgnoreCase.

Next:

Notice this method is accepting IEnumerable<string> and returning it, but the actual type is List<string>.

Here is how the final dictionary is used:

More inefficient LINQ usage. You just need a key value pair array and copy from the dictionary to it. The sorting can be implemented by using y.Weight.CompareTo(x.Weight).

More questionable methods:

Isn’t it just i.ToString(“N0”).Replace(‘,’, ‘ ‘)?

What kind of code quality you are really spending time on?