NDepend and IL instructions

I am currently running NDepend on various .NET solutions, to get into the habit of interpreting the various metric results.

Today, NDepend warned me that a GetHtml method was too complex. It had 206 IL instructions, where 200 is NDepend’s default upper limit. Since this was the only method that NDepend considered too complex, I decided to check it out.

The method was quite straightforward, but one obvious improvement would be replace all conditional appends. However, it only reduced the number of IL instructions with one per removal and resulted in a method with almost twice as many lines as the original one.

I therefore decided to restore the method to its original state and look elsewhere.

Next, a IsNullOrEmpty string extension method turned out to be a small contributor. Instead of being a plain shortcut to String.IsNullOrEmpty, it automatically handled string trimming as well, which made it a bit more complex.

I now realized that this auto-trimming was crazy, bad and really, really wrong, so I decided to rewrite the method and watched the number of IL instructions drop to 202. Still, we need to improve  the code further.

Another improvement would be to tweak how comparisons were made. After re-running the analysis, I then had a green light! The number of instructions was now 197. NDepend was satisfied. Still…197? Seems like many instructions for a method with 15 lines of code.

After some internal discussions, a colleague of mine suggested that I should extract the various attribute handlings to separate methods…and the number of IL instructions dropped to 96.

Can we reduce them further? 🙂