Cyclomatic Complexity is a measurement of the number of possible paths through a section of code. For example, a simple if/else would give you two paths through a section of code. This code would be easy to understand and test. As the number of choices increases, the difficulty of ensuring correctness on all possible paths increases even faster.
Using the Intel® Parallel Studio XE 2013, you can measure cyclomatic complexity. Intel® Composer XE 2013 generates the Cyclomatic Complexity metrics. Intel® Inspector XE 2013 provides the ability to visualize these Cyclomatic Complexity metrics.
We will be using a sample application called tachyon_ssa. You can download it from http://software.intel.com/en-us/articles/tachyon-ssa-samples. Unzip this application to a writable directory on your disk.
First we get set up for static analysis. This is done differently on different operating systems. For Windows*OS, read the following section. For Linux* OS, go to Setting up for Static Analysis Using a make File on Linux* OS.
Setting up for Static Analysis Using the Microsoft Visual Studio* IDE on Windows* OS
We will go through the setup process using the Visual Studio* solution tachyon.sln that is supplied with the sample program. Start by opening this solution in the Visual Studio* IDE. If you are using Visual Studio* 2008 or Visual Studio* 2010, then the Visual Studio* IDE will need to convert the solution file from Visual Studio* 2005 format. Let it go ahead and do this conversion.
Before you can set the options on your configuration, you need to set the project to build with the Intel compiler using the correct options.
1. Right-click the tachyon project in the Solution Explorer, select the Intel C++ Composer XE 2011 > Use Intel C++… menu item and click OK on the confirmation dialog box. You will see that the icon for the project in the Solution Explorer has changed to indicate it will be built using the Intel compiler.
2. Right-click the tachyon project and select Properties….
3. Open the C++ > Diagnostics property page. You will see the following dialog box. Set the Level of Static Analysis option to All Errors and Warnings (/Qdiag-enable:sc3) and click OK.
For information on setting up applications built using a command line script or make file, see the following section explaining the setup process on the Linux* OS.
Setting up for Static Analysis Using a make File on Linux* OS
The sample application comes with a make file that can be used to build the application on the Linux* OS. To save time, we have included two versions of the make file. One is the original make file, tachyon.mk. This make file builds the application using the GNU C++ compiler. The other is the updated make file, tachyon_ssa.mk, that adds a new build target, SSA. The SSA build target is just like the debug build target except for these changes:
1. It builds using the Intel compiler.
2. It adds the additional option -diag-enable sc3.
3. It puts the intermediate files in the SSA subdirectory.
Building on Windows* OS or Linux* OS
Now that you are set up, all you have to do is build your SA build configuration to perform analysis. On the Windows* OS, you can do this by right-clicking the tachyon project in the Solution Explorer and choosing Build. On the Linux* OS, you can do this by performing the following steps:
1. Open a command shell.
2. Set the environment variables for the Intel compiler by executing the iclvars.sh script in the compiler bin directory, supplying the ia32 option.
3. Execute make –f tachyon_ssa.mk.
On the Windows* OS, the Intel Inspector XE automatically opens a new result as soon as the build completes. On the Linux* OS, type inspxe-gui to invoke the Intel Inspector XE and then use the File > Open menu to open the result. By default, the file you want to open is called r000sc.inspxe, and is contained in a directory named r000sc below the root directory of the tachyon project.
Initially the file opens to show a list of problems discovered by static analysis.
Click the Complexity Metrics tab.
Click the Open button to show the Cyclomatic Complexity HTML report in the default browser.
Cyclomatic Complexity (CC) metrics have two primary recommended uses in software development:
1. Make code more maintainable to reduce bugs. Code with CC measurements that exceed a threshold value could be reviewed and refactored to reduce complexity when appropriate. Code with measurements that exceed a higher threshold could be required to be refactored unless a formal review determines that the level of complexity is acceptable.
2. Estimate the number of tests required to thoroughly exercise all application paths.
Using the Intel Parallel Studio XE to examine the complexity of your source code can greatly improve your ability to evaluate your QA/QC program.