Tutorials : Measuring the Complexity of OO Systems :

Response For Class (RFC)

This is one of the famous Chidamber and Kemerer metrics. This metric deals with the interaction between the main class and the less attendant classes. It is essentially defined as:

RFC =  M+ R where
   M = no. of methods in the class.
   R = total number of other methods directly invoked from the M
Here's an example:

public class RegistrationManager {

    public void createRegistration(RegistrationData regData){

        DataAccessManager manager = new DataAccessManager();
        AuditManager auditManager = new AuditManager();
        //save the registration
        manager.saveRegistration(regData);
        //audit the creattion
        auditManager.createAuditRecord(regData);


    }


    public Registration findRegistration(String regNumber){
      
        DataAccessManager manager = new DataAccessManager();
        Registration reg = null;

        //find the registration
        reg = manager.findRegsitration(regNumber);

        return reg;

    }

}
As you can see, the RegistrationManager class itself has two methods. Additionally, it depends on the DataAccessManager and AuditManager classes and uses methods from those classes. Following the metrics formula, the Response for Class measure for RegistrationManager class can be counted as:

Total no of methods in RegistrationManager = 2
Total no of methods called in DataAccessManager = 2
Total no of method called in AuditManager = 1
So,

RFC(RegistrationManager) = 2 + 2 +1 = 5
You can see that, when a certain class has dependencies on too many other classes, it becomes complex to modify or maintain. Thus, a high value of RFC is discouraged. At the same time, the foundation of an OO system is based on class and object interaction, which makes it impossible to develop a OO system with zero RFC. The goal is to strike a balance and, when you design any OO system, to keep an eye on this metric and stop any class reaching too high a RFC value.

One thing to notice about the above RFC measure is that you're counting only the first level call outside the class. However, it's also possible that the called methods will make various other calls to various other classes. A modified version of RFC encourages you to count the full call hierarchy. Whether you use one or the other, these metrics will help you keep the system less complex and easier to maintain and test.

Weighted Method per Class

The previous metrics demonstrate how too much interaction among various classes can make a system extremely complex. A system or a particular class is also considered too complex when there are too many methods defined within the class. The Weighted Method Per Class metrics helps address this issue.

In one implementation, it is simply the total number of methods defined within a class. In a variant implementation, it is the sum of the cyclomatic complexities of all the methods defined within the class:


WMC 1 = Total number of methods defined within a class.
WMC 2 = Sum of Cyclomatic Complexities of all the methods.
No matter which measure you choose, a high WMC value indicates that a given class should probably be re-factored into more classes. The inherited methods from the super classes are also counted and any overridden method has to be counted, because that is considered a new implementation.

This measure helps you keep a class clean and cohesive in terms of its behaviour. Take a look at the previous RFC example: if you had defined all the methods of DataAccessManager and AuditManager within the RegistrationManager class, you would definitely achieve a higher WMC value, indicating that the class needs to be re-factored.

Better Software Design

It's important to understand that these metrics are not here solely for academic interests. For instance, extreme programming and test-driven development use some of these metrics to determine the testability of classes and methods. If you're unable to write proper tests, these metrics can help you find the problem. Using these metrics correctly ultimately leads to better software design.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.