Oct 31, 2008

Code Metrics for a developer

Here are some code metrics each developer should be tracking all along.

Cyclomatic complexity:
Cyclomatic complexity of a program is the count of the number of linearly independent paths of execution. If the source code contained no decision points ( such as 'if' 'switch' 'while' ), the complexity would be 1, FLAT CODE, since there is only a single path through the code. If the code has a single ‘if’ statement there would be two paths through the code, one path where the ‘if’ statement is evaluated as true and one path where the ‘if’ statement is evaluated as false. So the complexity increasing with number of decision points in your code. If you have not written your decision statements properly, it will lead you into unnecessary conditions and hence complexity of the program increases.

Measuring CC tells you 2 important things.
How many ways your execution may end up. If you have higher CC, then maybe you can re-write your logic to make fewer conditions.
It tells you how many test cases will be required to achieve full code coverage.

Depth of Inheritance:
It is the measure of the level of inheritance a given solution. The deeper a class is in the hierarchy, the greater the number of methods it is likely to inherit, making it more complex to predict its behavior. As we go down inheritance graph, refined classes constitute greater design complexity, since more methods and classes are involved. Deep Inherited types are by-products of an over engineered hierarchy and one should avoid this. It causes major performance hit.

Class Coupling:
Coupling is the measurement of code independence and re-usability. A module is considered highly coupled when resident classes relies on each one of the other classes. In highly coupled cases, change in one class forces a cascading changes in other classes, classes are hard to reuse or test because of their dependence on other modules.Usage of proper Creational pattern reduces the coupling between the classes. A Class should interact with another class through a stable interface and does not need to be concerned with its internal implementation.

I think every development environment has the feature of analyzing code metrics these days. Visual Studio team suite/ system has this feature of calculating code metrics from a solution.
If you track and address these issues at the earliest, it will save you from a lot of code re-factoring.

No comments:

Post a Comment