Visible Progress Home We make progress visible.
Search Site:
Resources > Source Metrics  
VB Quality Zone
VB6 Coding Tips
Source Metrics
Source Line Statistics
Cyclomatic Complexity
Process Tips
Testing Tips

Our Customers...
What Customers Say...

VS Law 2005 - VB.NET 2005 coding standards enforcement.
Try our products for free today.
VS Law .NET - VB.NET 2002/2003 coding standards enforcement.
Try our products for free today. VB Law - VB6 coding standards enforcement.
 
  VB Law - Quality assurance for Visual Basic source code.  
 

Cyclomatic Complexity Metrics

Cyclomatic complexity is a measure of software complexity first introduced by Thomas McCabe in 1976 and measures the number of linearly-independent paths through a program module. A program module is generally considered to be a procedure having a single entry and exit point. Each program instruction that can potentially cause the execution path to change, such as an "If ... Then" statement, increments the complexity measure.

For a detailed explanation of cyclomatic complexity, refer to the Software Engineering Institute.

Visual Basic Cyclomatic Complexity Computation

VB Law Workstation and the VB Law Batch Processor compute cyclomatic complexity for Visual Basic source code by starting with an initial value of one at the start of a procedure and incrementing the computed complexity for each occurrence of the following keywords/constructs:
  • Case expression
  • For ... Next
  • Do Until ... Loop
  • Do While ... Loop
  • If ... Then
  • ElseIf ... Then
  • While ... Wend
  • On ... GoTo
  • On ... GoSub
  • Cyclomatic complexity example showing possible code execution paths.

    Use During Development

    Research has shown that minimising complexity is a key to writing high quality code.

    If the cyclomatic complexity of software is measured regularly during construction then complex areas of code can be identified early in the development life cycle and re-structured to improve understanding and to reduce testing and future maintenance effort. The cyclomatic complexity measurement of a procedure equates to the number of tests required to execute all code paths through a procedure and is therefore of use to developers whilst unit testing.

    The cyclomatic complexity of existing software can also be measured to assess possible risk during modification and the level of testing required.

    Many organisations have found it useful to set a threshold complexity value that all developers must stay within - a value less than 10 is widely regarded as an acceptable value, although research has shown that for highly skilled developers, expressing commitment to increased testing, values less than 15 may occasionally be acceptable).

    Ease Of Understanding

    Studies show that the lower the cyclomatic complexity for a procedure, the easier it is for the evaluator to understand the intended purpose of the procedure.

    Software Testing Effort

    There is a direct relationship between the cyclomatic complexity value and the number of test cases that need to be devised and executed to fully test a procedure. Cyclomatic complexity values can therefore be used to identify which areas of the software will require greater testing effort than others. The lower the cyclomatic complexity, the easier it is to determine suitable tests and to perform them.

    Modification Risk Assessment

    The cyclomatic complexity of a procedure can also be used as an indicator of the possible risk associated with modifying a procedure. Higher complexity will normally result in higher cost and risk when making modifications. The following table assesses the risk of modification to a procedure:

    Cyclomatic Complexity

    Risk Assessment

    1-10

    Simple procedure, low risk.

    11-20

    More complex procedure, moderate risk.

    21-50

    Complex procedure, high risk.

    >50

    Untestable procedure, very high risk.

    How VB Law Can Help

    The VB Law Workstation Add-In automates code reviews from directly within the Visual Basic 6 Integrated Development Environment (IDE) while the VB Law Batch Processor automates code reviews independent of the Visual Basic IDE. Both VB Law Workstation and the VB Law Batch Processor can easily compute cyclomatic complexity and raise violations for procedures which exceed an agreed level. Visit our downloads page and trial VB Law for free today.