N depend & cql

580 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

N depend & cql

  1. 1. NDepend & CQL © April 2008 http://www.ndepend.com
  2. 2. Agenda: <ul><li>Introduction </li></ul><ul><li>CQL Queries & Constraints </li></ul><ul><li>Quality & Metrics </li></ul><ul><li>Dependencies Layering & Refactoring </li></ul><ul><li>Code Snapshot Comparison </li></ul><ul><li>Code Coverage </li></ul><ul><li>State & Mutability </li></ul><ul><li>Dead Code </li></ul><ul><li>Optimal Encapsulation </li></ul><ul><li>Naming Convention </li></ul><ul><li>Object model </li></ul><ul><li>Build Process Health </li></ul><ul><li>Future </li></ul>
  3. 3. Introduction <ul><li>The source code is the design : a need to reveal secrets kept in the structure of source code: </li></ul><ul><li>A need to understand the big picture with a top-down approach , no matter the size of the code base. </li></ul><ul><li>A need to rationalize the architecture of software by controlling the structural complexity (graph of dependencies). </li></ul><ul><li>A need to capture architectural constraints and to be advised ASAP of violation. </li></ul><ul><li>A need to measure the code quality and coverage . </li></ul><ul><li>A need to control the code evolution . </li></ul>
  4. 4. CQL Queries & Constraints <ul><li>Queries: </li></ul><ul><li>SELECT TOP 10 METHODS ORDER BY NbLinesOfCode DESC </li></ul><ul><li>SELECT METHODS WHERE NbLinesOfCode > 10 </li></ul><ul><li>SELECT METHODS WHERE IsDirectlyUsing ″System.String″ </li></ul><ul><li>SELECT FIELDS WHERE HasAttribute &quot;System.ThreadStaticAttribute&quot; </li></ul><ul><li>Constraints: </li></ul><ul><li>WARN IF Count > 0 IN SELECT METHODS WHERE NbLinesOfCode > 20 </li></ul>
  5. 5. Quality & Metrics Lines Of Code (LOC) and NbILInstructions <ul><li>SELECT METHODS ORDER BY NbLinesOfCode DESC </li></ul><ul><li>SELECT TYPES ORDER BY NbILInstructions DESC </li></ul><ul><li>Logical LOC vs. Physical LOC </li></ul><ul><li>class Program { </li></ul><ul><li>static void Main() { </li></ul><ul><li>string str = &quot;hello&quot;; </li></ul><ul><li>str.IndexOf(&quot;hello&quot;, 10, 13); </li></ul><ul><li>str.IndexOf( </li></ul><ul><li>&quot;hello&quot;, </li></ul><ul><li>10, </li></ul><ul><li>13); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  6. 6. Quality & Metrics Volume & Comment metrics <ul><li>Volume: </li></ul><ul><li>SELECT ASSEMBLIES ORDER BY NbNamespaces DESC </li></ul><ul><li>SELECT NAMESPACES ORDER BY NbTypes DESC </li></ul><ul><li>SELECT TYPES ORDER BY NbMethods DESC </li></ul><ul><li>SELECT TYPES ORDER BY NbFields DESC </li></ul><ul><li>Comment: </li></ul><ul><li>SELECT NAMESPACES ORDER BY NbLinesOfComment DESC </li></ul><ul><li>SELECT METHODS WHERE </li></ul><ul><li>NbLinesOfCode > 10 AND PercentageComments < 30 </li></ul>
  7. 7. Quality & Metrics Coupling metrics <ul><li>Afferant Coupling (Ca) Efferent Coupling (Ce) </li></ul><ul><li>SELECT TYPES ORDER BY TypeCa DESC </li></ul><ul><li>SELECT METHODS ORDER BY MethodCe DESC </li></ul><ul><li>SELECT NAMESPACES ORDER BY NamespaceCa DESC </li></ul><ul><li>Ranking </li></ul><ul><li>SELECT TYPES ORDER BY TypeRank DESC </li></ul><ul><li>SELECT METHODS ORDER BY MethodRank DESC </li></ul>
  8. 8. Quality & Metrics Cyclomatic Complexity <ul><li>Cyclomatic complexity is a popular procedural software metric equal to the number of decisions that can be taken in a procedure. </li></ul><ul><li>Concretely, in C# the CC of a method is the number of following expressions found in the body of the method: </li></ul><ul><li>if | while | for | foreach | case | default | continue | goto | && | || | catch | ternary operator ?: | ?? </li></ul><ul><li>Following expressions are not counted for CC computation: else | do | switch | try | using | throw | finally | return | </li></ul><ul><li>object creation | method call | field access </li></ul><ul><li>SELECT METHODS WHERE CyclomaticComplexity > 20 </li></ul><ul><li>SELECT METHODS WHERE ILCyclomaticComplexity > 25 </li></ul><ul><li>WARN If Count > 0 IN SELECT METHODS WHERE </li></ul><ul><li>NbLinesOfCode > 20 AND CyclomaticComplexity > 8 </li></ul>
  9. 9. Quality & Metrics Lack Of Cohesion Of Methods (LCOM) <ul><li>Single responsibility principle: A class should not have more than one reason to change. In such class, almost every methods access every fields. </li></ul><ul><li>Lack Of Cohesion Of methods (LCOM) metric: </li></ul><ul><li>SELECT TYPES </li></ul><ul><li>ORDER BY LCOM DESC </li></ul>
  10. 10. Quality & Metrics Others metrics <ul><ul><li>Method: </li></ul></ul><ul><ul><li>SELECT METHODS WHERE NbParameters > 5 </li></ul></ul><ul><ul><li>SELECT METHODS WHERE NbVariables > 10 </li></ul></ul><ul><ul><li>Field Size of Instance: </li></ul></ul><ul><ul><li>SELECT FIELDS WHERE SizeOfInst > 16 </li></ul></ul><ul><li>Level & Layering: </li></ul><ul><ul><li>SELECT NAMESPACES ORDER BY NamespaceLevel DESC </li></ul></ul>
  11. 11. Dependency Layering & Refactoring Dependency Structure Matrix (DSM) <ul><li>Browsing </li></ul><ul><li>Dependencies weight </li></ul><ul><li>CQL Queries generation </li></ul><ul><ul><li>‘ Box & Arrow’ graph production </li></ul></ul><ul><ul><li>Indirect dependencies mode </li></ul></ul>
  12. 12. Dependency Layering & Refactoring Impact analysis to plan Refactoring <ul><li>What if we need to replace Moco.Cecil by another framework: </li></ul><ul><ul><li>SELECT TYPES WHERE IsDirectlyUsing &quot;ASSEMBLY:Mono.Cecil&quot; OR IsDirectlyUsing &quot;ASSEMBLY:Mono.Cecil.Pdb&quot; </li></ul></ul><ul><ul><li>‘ Box & Arrow’ graph production. </li></ul></ul><ul><ul><li>Code impacted </li></ul></ul>
  13. 13. Dependency Layering & Refactoring .NET Components: Assemblies vs. Namespace <ul><li>Assemblies: Physical </li></ul><ul><ul><li>Cost at development time : poor compilation performance </li></ul></ul><ul><ul><li>Cost at deployment : more file to manage </li></ul></ul><ul><ul><li>Cost at runtime : CLR overhead per assembly basis </li></ul></ul><ul><li>Namespaces: Logical </li></ul><ul><ul><li>No cycles dependencies detection </li></ul></ul><ul><ul><li>Internal visibility level </li></ul></ul><ul><li>Key to be able to use namespaces as components: </li></ul><ul><li>Being able to control dependencies between namespaces </li></ul>
  14. 14. Dependency Layering & Refactoring Introduction to levelization <ul><li>Central design principle: </li></ul><ul><li>Getting rid of dependency cycles between components </li></ul><ul><li>A and B belongs to a dependency cycles: </li></ul><ul><ul><li>They can’t be developed and tested independently </li></ul></ul><ul><ul><li>They are an indivisible unit </li></ul></ul><ul><ul><li>They constitute a super component </li></ul></ul><ul><li>Diseconomy of scale phenomenon: one super component costs more than several smaller components. </li></ul><ul><li>Right size for a component : 500 to 2000 LOC </li></ul>
  15. 15. Dependency Layering & Refactoring Demo: Levelization / dependencies cycles
  16. 16. Code Snapshot Comparison Introduction to Build comparison <ul><li>SELECT METHODS WHERE WasChanged OR WasAdded OR WasDeleted </li></ul><ul><li>SELECT METHODS WHERE CommentsWereChanged OR VisibilityWasChanged OR CodeWasChanged </li></ul><ul><li>SELECT METHODS WHERE IsUsedDifferently OR IsUsedRecently OR IsNotUsedAnymore </li></ul><ul><li>SELECT METHODS WHERE IsObsolete OR BecameObsolete </li></ul><ul><ul><li>SELECT TYPES WHERE IsDirectlyUsing &quot;ASSEMBLY:Mono.Cecil&quot; OR IsDirectlyUsing &quot;ASSEMBLY:Mono.Cecil.Pdb&quot; </li></ul></ul><ul><ul><li>‘ Box & Arrow’ graph production. </li></ul></ul><ul><ul><li>Code impacted </li></ul></ul>
  17. 17. Code Snapshot Comparison Build Comparison CQL Constraints <ul><li>Force comment change: </li></ul><ul><li>WARN IF Count > 0 IN SELECT METHODS WHERE </li></ul><ul><li>CodeWasChanged AND !CommentsWereChanged </li></ul><ul><li>API ascendant compatibility: </li></ul><ul><li>WARN IF Count > 0 IN SELECT METHODS WHERE </li></ul><ul><li>IsInOlderBuild AND IsPublic AND (VisibilityWasChanged OR WasRemoved) </li></ul><ul><li>Interface immutability: </li></ul><ul><li>WARN IF Count > 0 IN SELECT TYPES WHERE </li></ul><ul><li>IsInterface AND IsPublic AND WasChanged </li></ul><ul><li>Force quality on change: </li></ul><ul><li>WARN IF Count > 0 IN SELECT METHODS WHERE </li></ul><ul><li>(CodeWasChanged OR WasAdded) AND </li></ul><ul><li>(NbLinesOfCode > 20 OR CyclomaticComplexity > 10 OR …) </li></ul>
  18. 18. Code Snapshot Comparison Build Comparison facilities <ul><li>Tier code change. </li></ul><ul><li>SELECT METHODS WHERE IsUsedRecently </li></ul><ul><li>SELECT METHODS WHERE IsNotUsedAnymore </li></ul><ul><li>SELECT TYPES WHERE IsUsedDifferently </li></ul><ul><li>SELECT NAMESPACES WHERE IsUsedDifferently </li></ul><ul><li>SELECT ASSEMBLIES WHERE IsUsedDifferently </li></ul><ul><li>Dependency/Coupling changes. </li></ul>
  19. 19. Code Coverage Code Coverage Metrics <ul><li>Coverage data gathered from </li></ul><ul><ul><li>VSTS 2005 & 2008 </li></ul></ul><ul><ul><li>NCover 2.x </li></ul></ul><ul><li>Metrics </li></ul><ul><ul><li>PercentageCoverage </li></ul></ul><ul><ul><li>NbLinesOfCodeCovered </li></ul></ul><ul><ul><li>NbLInesOfCodeNotCovered </li></ul></ul><ul><ul><li>PercentageBranchCoverage (Ncover only) </li></ul></ul>
  20. 20. Code Coverage Code Coverage Facilities <ul><li>Uncoverable attributes </li></ul><ul><li>Coverage file merging options: </li></ul><ul><ul><li>OR </li></ul></ul><ul><ul><li>XOR </li></ul></ul><ul><ul><li>AND </li></ul></ul><ul><ul><li>NONE </li></ul></ul>
  21. 21. State and Mutability State and mutability: Definition of immutable types <ul><li>Immutable types: types where instances’ state cannot be modified once created. </li></ul><ul><li>A stateless type (i.e a type without any instance field) is considered as immutable. </li></ul><ul><li>A type with at least one non-private instance field is considered as mutable (because such a field can be eventually modified outside the type). </li></ul><ul><li>A class that derives directly or indirectly from a mutable type is considered as mutable. </li></ul><ul><li>Enumeration, static type, type defined in framework assemblies and delegate classes are never considered as immutable. </li></ul><ul><li>Particularly, classes that derive directly or indirectly from a class defined in a tier assembly that is not the System.Object class, is never considered as immutable. </li></ul>
  22. 22. State and Mutability CQL facilities for State and mutability <ul><li>Ensuring that the type Namespace.Foo is immutable: </li></ul><ul><ul><li>WARN IF Count != 1 IN SELECT TYPES </li></ul></ul><ul><ul><li>WHERE ! IsImmutable AND FulleNameIs &quot;Namespace.Foo&quot; </li></ul></ul><ul><ul><li>Matching methods that breaks immutability (i.e that modifies instance fields): </li></ul></ul><ul><ul><li>SELECT METHODS FROM TYPES &quot;Namespace.Foo&quot; WHERE </li></ul></ul><ul><ul><li>ChangesObjectState AND !IsConstructor </li></ul></ul><ul><ul><li>Matching methods that modifies static fields: </li></ul></ul><ul><ul><li>SELECT METHODS FROM TYPES &quot;Namespace.Foo&quot; WHERE </li></ul></ul><ul><ul><li>ChangesTypeState AND !IsClassConstructor </li></ul></ul><ul><ul><li>Matching methods that modifies a particular field: </li></ul></ul><ul><ul><li>SELECT METHODS FROM TYPES &quot;Namespace.Foo&quot; WHERE </li></ul></ul><ul><ul><li>IsDirectlyWritingField &quot;Namespace.Foo.m_Field&quot; </li></ul></ul>
  23. 23. Potentially dead code <ul><li>WARN IF Count > 0 IN SELECT METHODS WHERE </li></ul><ul><li>MethodCa == 0 AND </li></ul><ul><li>! IsPublic AND </li></ul><ul><li>! IsEntryPoint AND </li></ul><ul><li>! IsExplicitInterfaceImpl AND </li></ul><ul><li>! IsClassConstructor AND </li></ul><ul><li>! IsFinalizer </li></ul><ul><li>WARN IF Count > 0 IN SELECT FIELDS WHERE </li></ul><ul><li>FieldCa == 0 AND </li></ul><ul><li>! IsPublic AND </li></ul><ul><li>! IsLiteral AND </li></ul><ul><li>! IsEnumValue AND </li></ul><ul><li>! NameIs &quot;value__&quot; </li></ul><ul><li>WARN IF Count > 0 IN SELECT TYPES WHERE </li></ul><ul><li>TypeCa == 0 AND </li></ul><ul><li>! IsPublic AND </li></ul><ul><li>! NameIs &quot;Program&quot; </li></ul>
  24. 24. Optimal Encapsulation <ul><ul><li>SELECT TYPES WHERE CoulBeInternal </li></ul></ul><ul><ul><li>SELECT METHODS WHERE CoulBePrivate </li></ul></ul><ul><ul><li>Danger: public constructor and serialization </li></ul></ul><ul><ul><li>SELECT NAMESPACES WHERE CoulBeInternal </li></ul></ul><ul><ul><li>Detecting usage of </li></ul></ul><ul><ul><li>System.Compilers.Services.InternalsVisibleToAttribute </li></ul></ul><ul><ul><li>SELECT METHODS WHERE ShouldBePublic </li></ul></ul>
  25. 25. Naming Conventions <ul><ul><li>Instance fields should begin with m_ </li></ul></ul><ul><ul><li>WARN If Count > 0 IN </li></ul></ul><ul><ul><li>SELECT FIELDS WHERE ! NameLike &quot;^m_&quot; AND ! IsStatic </li></ul></ul><ul><ul><li>Interface name should begin with I </li></ul></ul><ul><ul><li>WARN If Count > 0 IN SELECT TYPES </li></ul></ul><ul><ul><li>WHERE IsInterface AND !NameLike &quot;^I&quot; AND !IsNested </li></ul></ul><ul><ul><li>Types names should begin with an upper letter: </li></ul></ul><ul><ul><li>WARN If Count > 0 IN SELECT TYPES </li></ul></ul><ul><ul><li>WHERE ! NameLike &quot;^[A-Z] &quot; </li></ul></ul><ul><ul><li>Exception class should be suffixed with ‘Exception’ </li></ul></ul><ul><ul><li>WARN If Count > 0 IN SELECT TYPES </li></ul></ul><ul><ul><li>WHERE IsExceptionClass AND !NameLike&quot;Exception$&quot; </li></ul></ul><ul><ul><li>or </li></ul></ul><ul><ul><li>WHERE DeriveFrom &quot;System.Exception&quot; </li></ul></ul><ul><ul><li>AND !NameLike &quot;Exception$&quot; </li></ul></ul>
  26. 26. Object model <ul><li>Types that should be sealed </li></ul><ul><li>WARN IF Count > 0 IN SELECT TYPES WHERE </li></ul><ul><li>IsClass AND NbChildren ==0 AND !IsSealed AND !IsStatic </li></ul><ul><li>Classes that implement ‘ICompare<T>’ should be nested </li></ul><ul><li>WARN IF Count > 0 IN SELECT TYPES WHERE </li></ul><ul><li>Implement &quot;System.Collections.Generic.IComparer<T>&quot; </li></ul><ul><li>AND !IsNested AND !IsInFrameworkAssembly </li></ul><ul><li>OO Hierarcy: </li></ul><ul><ul><li>SELECT TYPES WHERE DepthOfInheritance > 6 </li></ul></ul><ul><ul><li>SELECT TYPES ORDER BY NbChildren Desc </li></ul></ul>
  27. 27. Build Process Health <ul><li>Assemblies versionning issues such as </li></ul><ul><ul><li>AssemblyA references AssemblyB v2.1 but only AssemblyB v2.0 is available. </li></ul></ul><ul><li>Assembly conflicts such as: </li></ul><ul><ul><li>Several different assemblies with the same name can be found (different = different hash code and/or different version). </li></ul></ul><ul><li>PDB files issues such as: </li></ul><ul><ul><li>Missing PDB files. </li></ul></ul><ul><ul><li>PDB files and code source files not in-sync. </li></ul></ul><ul><ul><li>PDB files and assemblies are not in-sync. </li></ul></ul><ul><li>Coverage files issues such as: </li></ul><ul><ul><li>Corrupted or missing coverage files. </li></ul></ul><ul><ul><li>Coverage files and code source files not in-sync. </li></ul></ul><ul><ul><li>Coverage files and assemblies are not in-sync. </li></ul></ul>
  28. 28. Future <ul><li>Enhanced Visualization (2008 Q2) </li></ul><ul><li>XDepend = NDepend on Java (2008 Q3) </li></ul><ul><li>Code Query Framework CQF (NDepend 3, 2008 Q3) </li></ul><ul><li>Writing code query and constraints with C# and LINQ </li></ul>
  29. 29. Question?

×