Code Query Language 1.8 Specification

CQL 1.8

 

 

CQL 1.8 is supported by NDepend 2.9 and higher

http://www.NDepend.com

 

 

Copyright SMACCHIA.COM S.A.R.L 2006/2007/2008/2009

All right reserved

 

 


 

Content

Content 2

Introduction. 5

CQL and VisualNDepend. 5

Learning the CQL language. 6

The CQL language and real-world needs. 7

Storing CQL queries and constraints in your C# or VB.NET source code. 7

Some examples of queries and constraints written in CQL. 8

Examples of code Quality constraints. 8

Examples of naming constraints. 10

Examples of design constraints. 10

Examples of encapsulation constraints. 10

Examples of queries on the graph of dependencies. 11

Examples of queries on the inheritance tree. 12

Examples of queries to get extremum... 13

CQL queries elements. 14

SELECT METHODS. 14

SELECT FIELDS. 14

SELECT TYPES. 14

SELECT NAMESPACES. 15

SELECT ASSEMBLIES. 15

WARN IF xxx IN: Query vs. Constraint 15

TOP xxx: Restrict the number of rows in the result 15

FROM / OUT OF xxx: Domain of search. 16

WHERE xxx: Define a set of conditions. 16

ORDER BY xxx: Order rows of the result 17

Comments. 17

Query naming. 17

CQL name conditions. 17

Kind of Code Elements’ Names Prefixes. 20

The OPTIONAL: Code Elements’ Names Prefix. 20

NameLike “regex”  ANTMF. 21

NameIs “name”  ANTMF. 21

FullNameLike “name”  ANTMF. 21

FullNameIs “name”  ANTMF. 21

CQL metric conditions. 22

NbLinesOfCode  ANTM... 22

NbILInstructions  ANTM... 23

NbLinesOfComment  ANTM... 23

PercentageComment  ANTM... 24

NbMethods  ANTM... 24

NbFields  ANTF. 25

NbTypes ANT. 25

NbNamespaces AN.. 25

NbInterfacesImplemented  T. 26

NbParameters M... 26

NbVariables  M... 26

NbOverloads M... 27

CyclomaticComplexity  TM... 27

ILCyclomaticComplexity  TM... 28

ILNestingDepth  M... 28

SizeOfInst  TF. 29

TypeRank  T. 29

MethodRank  M... 30

DepthOfInheritance  T. 30

NbChildren  T. 30

FieldCa F. 31

MethodCe M... 31

MethodCa M... 31

TypeCe  T. 32

TypeCa  T. 32

NamespaceCe N.. 33

NamespaceCa N.. 33

ABT  T (Association Between Types). 33

LCOM  T (Lack Of Cohesion Of Methods). 34

LCOMHS  T (Lack Of Cohesion Of Methods Henderson-Sellers). 34

AssemblyLevel A.. 35

NamespaceLevel N.. 35

TypeLevel T. 35

MethodLevel T. 35

Abstractness  A.. 36

Instability  A.. 36

NormDistFromMainSeq  A.. 36

DistFromMainSeq  A.. 37

RelationalCohesion  A.. 37

AsmCa  A.. 37

AsmCe  A.. 38

CQL test coverage metrics conditions. 38

PercentageCoverage  ANTM... 38

NbLinesOfCodeCovered  ANTM... 39

NbLinesOfCodeNotCovered  ANTM... 39

PercentageBranchCoverage  M... 39

IsExcludedFromCoverage ANTM... 40

CQL code structure conditions. 41

IsUsing  ANTM... 41

DepthOfIsUsing  ANTM... 42

IsDirectlyUsing  ANTM... 42

IsUsedBy  ANTMF. 43

DepthOfIsUsedBy  ANTMF. 43

IsDirectlyUsedBy  ANTMF. 44

CreateA  M... 45

DepthOfCreateA  M... 45

DeriveFrom  T. 45

DepthOfDeriveFrom  T. 46

Implement  T. 46

HasAttribute  ATMF. 46

IsOfType  F. 46

ReturnType  M... 46

CQL boolean conditions. 47

IsPublic  TMF. 47

IsInternal  TMF. 47

IsProtected  TMF. 47

IsProtectedOrInternal  TMF. 47

IsProtectedAndInternal  TMF. 48

IsPrivate  TMF. 48

IsInFrameworkAssembly  NTMF. 48

IsFrameworkAssembly  A.. 48

IsEnumValue  F. 48

IsConstructor  M... 49

IsPropertySetter  M... 49

IsPropertyGetter  M... 49

IsStatic  TMF. 49

IsOverloaded  M... 49

IsVirtual  M... 50

IsAbstract  TM... 50

IsExplicitInterfaceImpl  M... 50

IsUsingBoxing  TM... 50

IsUsingUnboxing  TM... 50

IsUsingPinning  TM... 51

IsGeneric  TM... 51

IsUsingPointers  TM... 51

IsFinalizer  M... 51

HasFinalizer T. 51

HasLevel ANTM... 52

ContainsNamespaceDependencyCycle A.. 52

ContainsTypeDependencyCycle T. 52

ContainsMethodDependencyCycle M... 52

IsEntryPoint  M... 52

IsOperator  M... 53

IsIndexerSetter  M... 53

IsIndexerGetter  M... 53

IsLiteral  F. 53

IsInitOnly  F. 53

IsEventAdder  M... 54

IsEventRemover  M... 54

IsEventDelegateObject  F. 54

IsClassConstructor  M... 54

IsClass  T. 54

IsStructure  T. 55

IsEnumeration  T. 55

IsInterface  T. 55

IsSealed  T. 55

IsNested  T. 55

IsSerializable  T. 56

IsDelegate  T. 56

IsAttributeClass  T. 56

IsExceptionClass  T. 56

IsGeneratedByCompiler  TMF. 56

IsObsolete  TMF. 57

IsSpecialName  TMF. 57

CQL boolean conditions dedicated to Build Comparison. 57

WasAdded  ANTMF. 57

WasRemoved  ANTMF. 58

CodeWasChanged ANTM... 58

CommentsWereChanged ANTM... 59

VisibilityWasChanged TMF. 59

WasChanged  ANTMF. 59

BecameObsolete TMF. 60

IsUsedRecently ANTMF. 60

IsNotUsedAnymore ANTMF. 60

IsUsedDifferently ANT. 60

IsInNewerBuild ANTMF. 61

IsInOlderBuild ANTMF. 61

CQL boolean conditions dedicated to Optimal Encapsulation. 61

CouldBeInternal  NTMF. 62

CouldBeInternalProtected  TMF. 62

CouldBeProtected  TMF. 62

CouldBePrivate  TMF. 63

ShouldBePublic  TMF. 63

CQL boolean conditions dedicated to Purity / Side-Effects / Mutability. 64

IsImmutable  TF. 64

ChangesObjectState  M... 65

ChangesTypeState  M... 65

ReadsMutableObjectState  M... 66

ReadsMutableTypeState  M... 66

ReadsImmutableObjectState  M... 66

ReadsImmutableTypeState  M... 66

IsWritingField  M... 67

DepthOfIsWritingField  M... 67

IsDirectlyWritingField  M... 67

CQL 1.1 new features. 68

CQL 1.2 new features. 68

CQL 1.3 new features. 69

CQL 1.4 new features. 69

CQL 1.5 new features. 70

CQL 1.6 new features. 70

CQL 1.7 new features. 70

CQL 1.8 new features. 71

 

Introduction

CQL is a language which allows writing queries on the code structure of any.NET application, independently of the .NET language used (C#, VB.NET, C++/CLI …). For example, the following CQL query returns all the methods of your application with more than 200 IL instructions, ordered from the biggest to the smallest:

 

SELECT METHODS WHERE NbILInstructions > 200 ORDER BY NbILInstructions DESC

 

You might wish to avoid methods with more than 200 IL instructions since they are hard to maintain. After having shrunk all your methods, you certainly want to be notified when during development a method exceeds this threshold. The CQL language addresses this need by allowing the transformation of queries into constraints. For example, here is our previous query rewritten as a constraint:

 

WARN IF Count > 0 IN SELECT METHODS WHERE NbILInstructions > 200 ORDER BY NbILInstructions DESC

 

With almost a hundred keywords, the CQL language allows you to deal with various conditions on your code structure. It allows to write code quality constraints, naming constraints, design constraints, encapsulation constraints, queries on the graph of dependencies, queries on the inheritance tree,  queries to get the biggest or smallest code elements according to almost 30 metrics and much more.

 

CQL and VisualNDepend

The tool VisualNDepend allows the editing and execution of CQL queries and constraints. A GUI allows you to have a unique understanding of your application. VisualNDepend can also be used to generate reports during each build of your application.

 

VisualNDepend2_Big

 

Learning the CQL language

Writing CQL queries and constraints is straightforward thanks to the three following features:

 

VisualNDependCQLEditor

 

VisualNDependCQLEditor2

The CQL language and real-world needs

The CQL language has been conceived to understand and control real-world application. In a real-world environment, there are often exceptions (like automatically generated methods which are often very big) and you need to allow a few particular methods to exceed this threshold without being bothered by our previous constraint. The CQL language offers numerous features allowing you to deal with such exceptions. For example, all generated methods might contain the word “Generated” in their names:

 

WARN IF Count > 0 IN SELECT METHODS WHERE  NbILInstructions > 200 AND !NameLike "Generated" ORDER BY NbILInstructions DESC

 

Or maybe, all generated methods are in dedicated assemblies, namespaces or types:

 

WARN IF Count > 0 IN SELECT METHODS OUT OF NAMESPACES "MyApp.Generated1","MyApp.Generated2" WHERE NbILInstructions > 200 ORDER BY NbILInstructions DESC

 

Or maybe, you prefer to mention each one explicitly:

 

WARN IF Count > 0 IN SELECT METHODS WHERE NbILInstructions > 200 AND !(NameIs "Method1(Int32)" OR FullNameIs "MyApp.MyType.Method2(String)" ) ORDER BY NbILInstructions DESC

 

You can also mix all these features in the same constraint:

 

WARN IF Count > 0 IN SELECT METHODS OUT OF NAMESPACES "MyApp.Generated1","MyApp.Generated2" WHERE NbILInstructions > 200 AND !(NameIs "Method1(Int32)" OR FullNameIs "MyApp.MyType.Method2(String)" ) AND !NameLike "Generated" ORDER BY NbILInstructions DESC

Storing CQL queries and constraints in your C# or VB.NET source code

NDepend stores your CQL queries and constraints in the project file. As the source code is the design you might prefer storing your CQL queries and constraints directly in your source code (C#, VB.NET...). To do so, you must reference the assembly ./Lib/NDepend.CQL.dll and use the attribute NDepend.CQL.CQLConstraintAttribute in your code.

Some examples of queries and constraints written in CQL

Examples of code Quality constraints

WARN IF Count > 0 IN SELECT METHODS WHERE NbILInstructions > 200 ORDER BY NbILInstructions DESC

// METHODS WHERE NbILInstructions > 200 are extremely complex and

// should be split in smaller methods

// (except if they are automatically generated by a tool).

 

WARN IF Count > 0 IN SELECT METHODS WHERE ILCyclomaticComplexity > 40 ORDER BY ILCyclomaticComplexity DESC

// METHODS WHERE ILCyclomaticComplexity > 20 are hard to understand and maintain.

// METHODS WHERE ILCyclomaticComplexity > 40 are extremely complex and should be split

// in smaller methods (except if they are automatically generated by a tool).

 

WARN IF Count > 0 IN SELECT METHODS WHERE NbParameters > 5 ORDER BY NbParameters DESC

// METHODS WHERE NbParameters > 5 might be painful to call and might degrade performance.

// You should prefer using additional properties/fields to the declaring type to handle

// numerous states. Another alternative is to provide a class or structure dedicated to

// handle arguments passing (for example see the class System.Diagnostics.ProcessStartInfo

// and the method System.Diagnostics.Process.Start(ProcessStartInfo))

 

WARN IF Count > 0 IN SELECT METHODS WHERE NbVariables > 15 ORDER BY NbVariables DESC

// METHODS WHERE NbVariables > 8 are hard to understand and maintain.

// METHODS WHERE NbVariables > 15 are extremely complex and should be split in

// smaller methods (except if they are automatically generated by a tool).

 

WARN IF Count > 0 IN SELECT TYPES WHERE NbMethods > 20 ORDER BY LCOM DESC

// TYPES WHERE NbMethods > 20 might be hard to understand and maintain

// but there might be cases where it is relevant to have a high value for NbMethods.

// For example, the System.Windows.Forms.DataGridView standard class has more than 1000 methods.

 

WARN IF Count > 0 IN SELECT TYPES WHERE NbFields > 20 AND !IsEnumeration

// TYPES WHERE NbFields > 20 AND !IsEnumeration might be hard to understand and maintain

// but there might be cases where it is relevant to have a high value for NbFields.

// For example, the System.Windows.Forms.Control standard class has more than 200 fields.

// The value of the metric SizeOfInst might be a better indicator of complex type.

 

WARN IF Count > 0 IN SELECT TYPES WHERE SizeOfInst > 64 ORDER BY SizeOfInst DESC

// TYPES WHERE SizeOfInst > 64 might degrade performance (depending on the number of

// instances created at runtime) and might be hard to maintain.

// However it is not a rule since sometime there is no alternative

// (the size of instances of the System.Net.NetworkInformation.SystemIcmpV6Statistics

// standard class is 2064 bytes).

 

WARN