Optimizing Coupling and
Cohesion in C++ and Java

Analyzing Alternative Class Encapsulations in C++ and Java

Encapsulation is the major means of achieving cohesion in an object oriented program. Suboptimal encapsulation, such as too many objects within a class, unrelated objects within a class, or tightly related objects spread across too many classes, can lead to problems in the extensibility, testability and reuse of the software.

Imagix 4D's class level metrics, particularly the Chidamber and Kemerer metrics, provide insight into the overall quality of a system's object oriented design. They also highlight specific areas where coupling and cohesion are a problem. You can then use Imagix 4D source code analysis and visualization features to study the specifics of your code's fundamental design and implementation.

The particular focus of this app note is how, working with existing source code, you can experiment with alternative encapsulation strategies and study the impact they would have on your code's architecture.
One of Imagix 4D's built in abstraction features is that symbol level relationships are abstracted to the symbols' containers. So if classA::methodA calls classB::methodB, classA is considered to call classB. The same is true for files. This is the basis of Imagix 4D's Class Calls and File Calls views.

In this Class Calls view, you can quickly see the classes calling into and called from CFolderScanItem.

From a given Class Calls view, you can drill down to more detail by switching to a UML Class Diagram. There, you'll be able to examine which specific methods are being called between classes.

Switching to the equivalent UML Class Diagram, information about all of the inter-class function calls becomes overwhelming; hence the need for abstraction. The next step would be to focus the view on a smaller set of classes and examine their relationships. (+ larger)

Imagix 4D has one additional container, groups, which enables you to create a user-defined collection of symbols. Just as for classes and files, abstraction of relationships occurs for groups. You can create groups of methods/functions and study the resulting interfaces at the higher abstract level by looking at the calls (and/or sets and/or reads) between groups.

Through a simple right-click menu action, you can create groups that duplicate the membership of a given class. This makes it easy to start with groups that mimic the current class encapsulation.

Notice that while the overall relationships are the same as for the Class Calls graph above, the shapes and the names have changed as the encapsulation is now being analyzed through the groups mechanism.

The relationships between the groups represent the coupling between classes, and you're able to see these at the higher level abstraction. You can to examine the detailed relationship between pairs of groups to understand the specific functions (methods) and variables (attributes) that are involved.

Through Imagix 4D's Analyze feature, you can examine the inter-class associations in detail. Here, the calls between CQueueView members and CFolderScanItem members are displayed.

Imagix 4D's grouping mechanism makes it easy to move functions and variables from one container to another. In this way, you can graphically analyze what would happen to the coupling and cohesion if you were to change your class partitioning. You're able to experiment with different encapsulation strategies quickly and easily, before you change a single line of code.

By moving members between groups, and even splitting or merging groups, you can experiment with different encapsulation strategies. Here, the impact of splitting CFolderScanItem is seen.