English 中文(简体)
How to restrict NDepend methods query on type attribute
原标题:

I m trying to get NDepend to identify long methods using a modified version of the standard "Methods too big" query.

I don t want to report long methods that the developers have little control over, so I filter out generated code using the DebuggerNonUserCode attribute and InitializeComponent().

Unfortunately, I still get a few false positives as methods in generated types are reported as well. The problem is that while the type itself has the DebuggerNonUserCode attribute, the methods do not, so they are included in the output despite the fact that they are generated.

I am looking for something like a join between types and methods: Give me all types that do not have the DebuggerNonUserCode attribute and run the query on those, but I can t figure out how to express this in CQL.

For some of the assemblies, I can simply filter on the full name, but unfortunately some of our assemblies mix developer made and generated types. Unfortunately the IsGeneratedByCompiler can t be used either in this case.

My query

WARN IF Count > 0 IN SELECT METHODS WHERE 
   NbLinesOfCode > 30 AND
   !HasAttribute "System.Diagnostics.DebuggerNonUserCodeAttribute" AND
   !NameIs "InitializeComponent()"
   ORDER BY NbLinesOfCode DESC
最佳回答

Brian, thanks to Code Rule over LINQ Query (CQLinq) the source code of the code rule you are asking for is:

warnif count > 0
from m in Application.Methods where
  m.NbLinesOfCode > 30 &&
 !m.ParentType.HasAttribute( "System.Diagnostics.DebuggerNonUserCodeAttribute") &&
  m.Name != "InitializeComponent()"
orderby m.NbLinesOfCode descending
select new { m, m.NbLinesOfCode }

The CQLinq syntax also offer a way to define what is Just-My-Code. This feature is describe here. Basically you need to define the set JustMyCode through queries prefixed with notmycode. Then the rule you are asking for can be rewritten easily:

warnif count > 0
from m in JustMyCode.Methods where
   m.NbLinesOfCode > 30
orderby m.NbLinesOfCode descending    
select new { m, m.NbLinesOfCode }

The set JustMyCode defined once for all can be reused over any code rule. Additionaly you can look at the notmycode default query Discard generated and designer Methods from JustMyCode

问题回答

I quite like NDepend, but it s still the biggest single shortcoming that namespace/type/method info cannot be joined into a single query. That feature would make CQL really powerful stuff.

Apart from that the checks IsGeneratedByCompiler and IsInFrameworkAssembly may be helpful. You can also remover certain namespaces from the query (OUT OF NAMESPACES "...")





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签