Entries tagged as design qualityRelated tags checkstyle continuous integration cruisecontrol cyclomatic complexity npath complexity php phpunit php_codesniffer php_depend pmd quality assurance software metrics arbit conference ipc karlsruhe kore talks bug fix php5 release announcement overview pyramid software design ant book changes company consulting crazy css dortmund elephpant ext/filter ezcomponents fabien potencier fun hamburg lint logger phpbbq phpmd phpt phpug phpugdo phpundercontrol pirum quality software architecture static code analysis static_reflection support testing thinkpad tools training unconference unit test xml xpath zce zend certified engineer cbo class interface size coupling between objects dbus derick optimization performance tokenizer annotations cli customization dashboard phpdoc rule set usability elger git github mayflower php_codebrowser sebastian marek thorsten article cologne php-magazinTuesday, July 7. 2009The value of complexity metrics - Cyclomatic Complexity (1/2)Software metrics are currently on everyone's lips and a frequently discussed topic. There are many conference talks, blog posts and other presentations that talk about software metrics. But to me it seems as if this subject is a closed book for many developers, so I decided to write this little post about a special category of software metrics, the complexity metrics. Complexity metrics are a theoretical approach to measure the subjective complexity of a software fragment, where the words software fragment stand for a paraphrase for functions, methods, classes and nearly every logical unit that can be found in a software system. The most prevalent procedure to calculate complexity values is static code analysis, where an application parses the raw source code of a project, counts different statements and expressions and packs up the determined results in simple classification numbers. And with this information you already know the main concepts behind most software metrics, classification numbers and counting. As you can see there is no magic behind the scene, the only thing required is a good background knowledge to interpret those values. The Cyclomatic Complexity Number or short CCN is the oldest complexity metrics. The first time this software metric was mentioned was 1976 by Thomas J. McCabe. This metric counts the available decision paths in a software fragment to determine its complexity. Each decision path starts with one of the conditional statements from the following list, so that it is fairly easy to detect them in existing source code.
A look at this list of statements may result in the questions:
Is this list wrong, it doesn't list Each decision path gets the value 1 and the sum of all these values represents the Cyclomatic Complexity of the analyzed software fragment. Note that each function and method also counts with a value of 1 With this knowlegde we can now calculate the complexity of the following example code:
Based on the previous definition the Cyclomatic Complexity
Number of the example code example is 5. But you may have
noticed that this approach does not capture all decision paths
that exist. We haven't catched those paths that came from the
by the boolean expression Now we get a complexity value of 8 when we apply the CCN2 to the previous example, what is a growt of the software's complexity of 60%.
Due to the fact that Cyclomatic Complexity Number was
originally invented for procedural programming languages,
this definition for the Cyclomatic Complexity Number still
misses one element to measure the complexity of an object
oriented software system. With the concept of exceptions a
software gets additional decision paths for each
Now that we know what the Cyclomatic Complexity Number is, what can we do with the measured information? We can find the complexity hotspots in a system, for example the top ten artifacts with the highest complexity, but this is only important during an initial analyses phase to get the big picture of an application. For a continuous inspection this information is not so important. A continuous analyses requires thresholds that help to categories calculated values. During the time four values have emerged as good thresholds for the Cyclomatic Complexity Number of a software system.
You may ask, why should I care about the complexity of a software system, where is the value of benefit in this metric? Mostly the complex parts of an application contain business critical logic. But this complexity has negative impacts on the readability and understandability of source code. Those parts will normally become a maintainence and bug fixing nightmare, because no one knows all the constraints, side effects and what's exactly going on in that part of the software. This situation results in the well known saying "Never touch a running system" which in turn mostly ends in copy&paste programming. The situation can even become more critical when the original author leaves the development team or the company. Finally a small example how to apply the new knowledge about the Cyclomatic Complexity Number, thresholds and the negative impacts of complex software to an existing development process. The following source listing shows a complex method taken from PHP_Depend's source. This method has a Cyclomatic Complexity Number of 16 and I must admit that the original author needed some time to understand what was going on in this method.
The first thing to do is to make sure that the test suite is good enough to ensure that the required refactorings will not change the public behavior of the component or class. When this is donw and we are sure our that api breaks will be detected by the test suitewe can start to extract logic into separate methods. The following example shows the result of the refactoring:
The subjective feeling of readability heavily depends on the complexity of control structures, as we can see by a comparison of the original and the refactored version of the method example. The new version with its Cyclomatic Complexity Number of 5 is much easier to read and understand. This text is the first of two blog posts. The second article will give a short introduction into the NPath Complexity You liked this article and you are interested in this and other quality assurence related topics? - Then you should now order your copy of the Book Quality Assurance in PHP Projects. The book talks about nearly all aspect of quality assurence, with practical tips and expert knowledge contributed by certain PHP professionals.
Posted by Manuel Pichler
in php, php_depend, phpugdo, phpundercontrol, planet-php, projects
at
21:20
| Comments (2)
| Trackbacks (0)
Defined tags for this entry: checkstyle, cyclomatic complexity, design quality, npath complexity, php_depend, quality assurance, software metrics
Monday, July 21. 2008New PHP_Depend release 0.8.0beta4This should be the final beta release for PHP_Depend 0.8.0. Beside some minor bugfixes, it contains a last API change for the log sub component. This change removes constraints to the ctor signature of a logger implementation, which is now part of a separate interface. The following list shows all changes in this version. A detailed description of all issues can be found in the PHP_Depend issue tracker!
As always you can use PHP_Depend's pear channel or its svn repository, to check out this new version. And feel free to discuss and follow the PHP_Depend development on its dedicated mailing list.
Posted by Manuel Pichler
in php, php_depend, phpugdo, phpundercontrol, projects
at
20:47
| Comments (0)
| Trackbacks (0)
Defined tags for this entry: design quality, php, php_depend, quality assurance, release announcement, software metrics
Saturday, June 21. 2008What you should know about PHP_DependHere a short follow up for the PHP_Depend release announcement, with hints on using PHP_Depend.
First I must explain that PHP_Depend is working package oriented. This means it heavily relies on
With default settings PHP_Depend suppresses the +global Package, because it expects that this package contains classes from external libraries, like Zend Framework or eZComponents. If you use PHP_Depend for a project with less or no The second important hint is related to exported charts and images. If your operation system, image viewer or browser supports SVG, you should use this format. The reason for this decision is really simple, SVG provides tooltips and so you get most informations from the generated graphics. For the Overview-Pyramid this means, that you get a tooltip for the used acronyms. ![]() PHP_Depend Overview-Pyramid with acronym tooltip And within the JDepend-chart you get the package name as tooltip. ![]() PHP_Depend JDepend-Chart with package tooltip
I hope this short introduction helps you while you are trying out PHP_Depend. If you are already using the metrics feature of PHPUnit, you should try out the new
Posted by Manuel Pichler
in php, php_depend, phpugdo, projects
at
23:35
| Comment (1)
| Trackbacks (0)
Wednesday, February 6. 2008How reusable, extensible and maintainable is your software?To provide a flexible and extendable software, it is a good OO practice to reduce the dependencies between implementing classes. This could be achieved by developing against abstractions which means both, abstract classes and interfaces. By using abstractions instead of real implementation in the application you provide some sort of contract, that could be used by others to hook into the application with their own classes that fulfill the contract. Except the extensibility of an application a good abstraction reduces the risk of breaks in multiple subsystems when something was changed in a single package. But how to get rid of all these dependencies, doing this by hand will become an impossible job, at least for larger projects. At this point a tool should be used to assist the development process. PHP_Depend is an adaption of the established Java development tool JDepend. This tool shows you the quality of your design in the terms of extensibility, reusability and maintainability. All these facts are influenced by the inter-package dependencies and the package abstraction that PHP_Depend visualizes in form of an abstract/instability chart and as a detailed XML report of all detected dependencies. PHP_Depend calculates the following metrics by counting classes, interfaces and dependencies.
With these values we can create a chart that visualizes the relationship between the instability (I) on the y-axis and the abstraction (A) on the x-axis. Because in a project not each package can achieve the optimal values of A = 1, I = 0 or A = 0, I = 1, we draw a diagonal between the two corners. This line is called the Main sequence and represents an average between abstraction (A) and instability (I). This means that packages near this line have a good mix between abstraction and instability. You can call these packages balanced. Because it is desirable for packages to be close to the Main sequence we have a fifth metric, the package distance (D) from this ideal.
If you are interested in this subject and PHP_Depend you can check out the latest version, including the unit tests, from the subversion repository or simply install the PHP_Depend PEAR package. But be aware this software is at a very early stage. mapi@arwen $ pear channel-discover pear.xplib.de mapi@arwen $ pear install xplib/PHP_Depend-alpha Now you can run PHP_Depend with the following command. pdepend <source-dir> [<output-dir>]
If you are interested in the code coverage follow this link I will close this post with a cite of Robert Martin:
The following sources were used for this post.
Posted by Manuel Pichler
in php, php_depend, phpugdo, phpundercontrol, planet-php, projects
at
17:52
| Comments (9)
| Trackback (1)
Defined tags for this entry: design quality, php, quality assurance, software architecture, software design, software metrics
(Page 1 of 1, totaling 4 entries)
|
ProjectsFurther stuffCategories |



