Run-Time Systems Cyclomatic Complexity Calculator for Delphi
About
Cyclomatic Complexity is a standard measurement of the overall complexity of source code based loosely on the number of loops and branches that are contained within the source module. The RTS Cyclomatic Complexity for Pascal (CCP) is an implementation of this procedure for Delphi and Pascal based source code.

I highly recommend you see Carnegie Mellon Software Engineering Institute for further details and links to other great resources for the systematic measurement of source code.
Description
If you are responsible for reviewing, maintaining, or testing pascal based source code the application of cyclomatic complexity can be used to assist you in determining areas of your source code that require the most attention. It provides you with a highly systematic and standardized measurement that can quickly focus you on the areas of your source code that can benefit from increased effort in refactoring and unit testing. This is provided with bias on source formatting preferences or other readability factors that can often tend towards personal preferences amongst individual developers.
I am sure we have all looked at a routine, a module, or a class and had to read through it more than twice to get some idea of what was going on. When this happens you immediately become more tentative about applying changes to that particular bit of code. In practice, this is often an indicator that the code should actually be modified to enhance your ability to maintain that segment of code. If you don't have any unit test cases you are much more likely to leave it along or worse yet, dive right in. By measuring the cyclomatic complexity of the routine before you start it will quickly give you an idea of the number of test cases that should exist to exercise the routine and whether the routine has reach a level of complexity that it should be seriously examined for reconstructive surgery (ala rewrite).
I spend more time reviewing and/or editing code that is not my own; and over time have been able to develop a decent sense of how complex a module is after a visual scan or two. The only problem with this is that no matter how hard you try you end up applying your personal formatting preferences as a penalty to the underlying code. This isn't fair and it certainly isn't following a great process. Using cyclomatic complexity has helped me be much more fair and better focus review efforts.
The other significant benefit of applying cyclomatic complexity measurements to your source code is that it provides a clear and statistical message that you can take to your management responsible for resourcing. With this information in hand you will have a much stronger case for allocation of resources to make internal software changes that cannot be directly related to software features or client requested change requests. Using these figures together with your developer's maintenance investment hours can be a geat way to focus your group on code improvements that pay in the long run.
The RTS Cyclomatic Complexity calculator is provided both in binary and source forms. If you are comfortable with compiling and installing source packages I recommend you start with the source package (besides, then you can tweak it). If you want to get started quickly and easily than just download the package that matches the version of Delphi you have, install it, and you are off to the races.
You can provide feedback, bugs, and suggestions by sending an e-mail to ryan @ run-time-systems.com. Please be sure to include CCP as part of the subject line otherwise it may not get noticed or claimed as spam.
Usage
For a single file
Open the file in the IDE and choose Tools | Cyclomatic Complexity.
For an entire project
Open the project in the IDE and choose Project | Cyclomatic Complexity.
Message View contents
After you have requested one of the above actions the results will be placed into the message view (the same place you see compiler errors). There will be a single entry for each source code file that was processed. Each file should have a + icon beside it that you can click on to see the details for each method within the file. The content that is displayed highly depends on the complexity of the module itself. The methods are categorized into low, medium, high, and ultra levels of complexity as is described on the Cyclomatic Complexity site listed in the reference section.
Cyclomatic Complexity Sample
You can double click on any method listed and jump to the method directly in the editor. You can also save the results by right clicking in the message view and choosing the Save Messages... menu option.
Pressing F1 while highlighting either a module or method will present you with a more detailed breakdown of the assessment of the source code.
Cyclomatic Complexity Sample
The Cyclomatic Complexity value can be viewed as providing a general measurement of the number of individual test cases that you will require to adequately cover that routine in unit testing. This value is generally composed as "branches + loops + inners + 1".
The Depth of nesting reflects the depth that loops embed within one another. The deeper the nesting the more complex it is to generally understand the logic within the module. This is especially true when the length of the procedure grows beyond a few lines of code.
The Possible branches taken represents the number of decision points in the routine and include constructs such as if, else, and case branches.
The Loop constructs provides the number of loop statements that exist within the routine such as for, while, and repeat.
Installation
The RTS CCP is provided as an installable Delphi Expert. Installation can be completed as follows:
  1. Download the package that matches your Delphi version
  2. Start Delphi
  3. Choose Component | Install Packages...
  4. Choose the Add... button and browse to the location where you saved the rtsccp_Dxx.bpl package and choose the Open button
  5. You are done.
  6. If you get errors it may be that you are having a unit name collision. If you are please let me know and/or download the source version and alter name unit names as required to get rid of the collision.
Download
Last Update: May 4, 2005 - 0.9.1.2006
Binary Only: Delphi 6, Delphi 7, Delphi 2005
Source Code
License
Portions of the source code are covered by the MPL license as indicated in the references section of this page. Use of those source code modules must comply with said license.
Source code modules supplied as part of this distribution that are the sole ownership and copyright of Run-Time Systems may be used in whatever manner you would like provided you accept that neither myself nor Run-Time Systems will accept any accountability or liability for such usage.
History
Known Issues
References