ConcernMorph: Metrics-based Detection of Crosscutting Patterns

Eduardo Figueiredo, Jon Whittle, and Alessandro Garcia


Abstract

Crosscutting concerns can hinder maintainability of a design because they do not adhere to a system’s underlying modular structure. Developers, therefore, may wish to refactor designs to improve modularisation or to implement crosscutting concerns as aspects. However, few tools currently exist that assist developers in detecting crosscutting concerns in their code. Furthermore, we are not aware of any tools that classify crosscutting concerns once they are found. Classification is important because, as recent studies have shown, crosscutting concerns are not always harmful. This paper describes a tool, ConcernMorph, for identifying crosscutting concerns and classifying them into one of a number of predefined crosscutting patterns.



ConcernMorph - Detecting Crosscutting Patterns - Credits


Detecting Crosscutting Patterns

This supplementary text presents a heuristic detection strategy to automate the process of identifying those crosscutting patterns. Thi approach is supported by ConcernMorph. The proposed detection strategy technique combines information from a complementary set of concern analysis techniques including concern-oriented [3, 4] and conventional metrics [2, 8], concern-based clone detection [1], and concern-based dependency between components [4]. We summarise below which techniques have been considered to identify each category of crosscutting patterns.

Concern-oriented metrics to identify flat crosscutting shapes

Inspired by detection strategies proposed by Marinescu [9], we define specific strategies to identify concerns with flat crosscutting shapes. Marinescu’s work uses conventional module-driven metrics aiming at detecting a set of traditional flaws, such as the Shotgun Surgery bad smell [5]. Here, the detection strategy mechanism is extended with recently proposed concern metrics [3, 4]. We present below a short description for six concern and conventional metrics used by the elaborated detection strategies. More information about those metrics can be found at the respective references.

Concern-oriented metrics

  • Concern Diffusion over Components (CDC) [4]: Counts the number of components that realise part of a given concern
  • Number of Concern Attributes (NOCA) [4]: Counts the number of attributes that contribute to the realisation of a concern
  • Number of Concern Operations (NOCO) [4]: Counts the number of methods and advices that participate in the concern realisation

Conventional size metrics

  • Number of Components (NC) [8]: Counts the number of classes, interfaces and aspects.
  • Number of Attributes (NOA) [2]: Counts the number of attributes of each class, interface or aspect.
  • Number of Operations (NOO) [2, 10]: Counts the number of methods and advice of each class or aspect.

Before presenting the three detection strategies aiming at identifying Black Sheep, Octopus, and God Concern, we also define two conditions A (Little Dedication) and B (High Dedication) used by the rules. We explicitly separate those two conditions from the strategies in order to reuse them in more than one detection strategy and also make the rules easier to understand.

Little Dedication

Condition A: (NOCA / NOA < 0.33) and (NOCO / NOO < 0.33)

High Dedication

Condition B: (NOCA / NOA = 0.67) and (NOCO / NOO = 0.67)

The first rule (R01) below identifies an instance of the Black Sheep crosscutting pattern if all components which have this concern dedicate only a few percentage points of attributes and operations to it (less than 33 %). In other words, it the condition A (Little Dedication) holds.

Black Sheep

R01: if (Little Dedication) for every component with CONCERN
then CROSSCUTTING CONCERN is BLACK SHEEP

The next rule (R02) verifies if a crosscutting concern not classified as Black Sheep is an Octopus candidate. According to this detection strategy, this crosscutting pattern occurs if every component realising parts of the concern has either little dedication (condition A) or high dedication (condition B) to it. Besides, at least one component have to be little dedicated to the concern (octopus’ tentacles) and at least one component has to be highly dedicated (octopus’ body). We define a component as highly dedicated to a concern when the percentage of attributes and operations are equal to or higher than 67 %.

Octopus

R02: if ((Little Dedication) or (High Dedication) for every component with CONCERN)
  and ((Little Dedication) for at least 1 component with CONCERN)
  and ((High Dedication) for at least 1 component with CONCERN)
then CROSSCUTTING CONCERN is OCTOPUS

Finally, the third detection strategy (R03) aims at finding God Concern. It warns the developer of a God Concern candidate if two conditions are satisfied: (i) if the given concern affects more than 33% of the system’s components and (ii) if most of those affected - not all - components are highly dedicated to that concern (condition B).

God Concern

R03: if (CDC / NC) > 0.33 and (High Dedication) for most components with CONCERN
then CROSSCUTTING CONCERN is GOD CONCERN


Tree-based algorithms to detect inheritance-wise concerns

Both Climbing Plant and Hereditary Disease crosscutting patterns are straightforwardly identified. Basically, we need two steps to spot them: (1) create a forest (i.e., a disjoint union of trees) including all inheritance trees which have at least one component realising the concern under consideration; and (2) verify which trees in the forest have at least one vertex not assigned to the concern, although every root must realise the given concern. Once those two steps are accomplished, trees with all vertices realising the concern are regarded to Climbing Plant. On the other hand, Hereditary Disease is identified by trees with at least one vertex not assigned to the concern.


Graph search algorithms to find communicative concerns

The first step to identify the communicative concerns, Tree Root, Tsunami, Neural Network, and King Snake, is to build for each concern under analysis a graph of connections, called graph of concern dependencies. The vertices of this graph are all components realising the analysed concern and the edges are coupling connections between a pair of components which are related to the concern. For example, The figure below presents the graph of concern dependencies for an implementation [6] of the Observer pattern. This figure also shows a partial code of the Point class in order to illustrate the two edges that connect this vertex to Subject and Observer. In other words, the shadowed areas of the Point class refer to the Subject and Observer interfaces what results in the two edges leaving the Point vertex.

After the creation of those graphs for each concern, our heuristic strategy to identify crosscutting patterns use variations of graph search algorithms, such as Breadth First Search (BFS) and Depth First Search (DFS) [7]. Tree Root and Tsunami can be found by an adapted BFS algorithm. Our BFS variant records the number of vertices that can be reached by each vertex. The figure below illustrates a simulation of this BFS algorithm in the sample graph of the above figure. The number of vertices reached by a given vertex is placed between brackets when a vertex becomes black (visited). Once the BFS execution has finished, the wave source of Tsunami is the vertex with the highest assigned value. Tree Root is identified using the same algorithm, but a graph with inverted edges is used as input. In the Tree Root case, the highest vertex value indicates a trunk.

Furthermore, the Neural Network and King Snake crosscutting patterns are detected by using graph algorithms in directed acyclic graph (DAG), i.e., a directed graph with no directed cycles. We implemented a DFS algorithm [7] that marks vertices it has already visited and does not visit them again. Hence, it transforms the graph of concern dependencies into a DAG. The resulting DAG includes all components participating in the Neural Network crosscutting pattern. To identify King Snake though, we need a step further. Our DFS variant marks the vertex with the longest path (deepest) from another arbitrary vertex. Therefore, we retrieve components in the longest path between two nodes and take them as composing the King Snake crosscutting pattern.


Clone detection techniques and structural analysis to identify other abusive concerns

Copy Cat and Dolly Sheep require the use of clone detection techniques [1] to find duplicated or similar pieces of concern code in different components. In fact, however, we have not yet implemented an algorithm to automate their detection. The other two crosscutting patterns, Data Concern and Behavioural Concern, are identified based on structural analysis of components realising the target concern. The heuristic strategy is, for every component, to investigate all members realising the concern. Then, a concern whose composing components have only attributes, class variable, or getting and setting methods to realise it is classified as Data Concern. Similarly, a concern which is composed of only methods and constructors (and advice in case of AspectJ programs) is considered Behavioural Concern. Concern-oriented metrics, such as NOCA and NOCO (presented above), can also be used to support the identification of Data Concern and Behavioural Concern. However, we cannot rely only on those metrics since they do not make any distinction between getting and setting methods from other ordinary operations.


References

  1. M. Bruntink, A. van Deursen, R. van Engelen, T. Tourwé. An Evaluation of Clone Detection Techniques for Identifying Crosscutting Concerns. In Proc. of the ICSM Conference, 2004.
  2. S. Chidamber and C. Kemerer. A Metrics Suite for Object Oriented Design. IEEE Trans. on Software Engineering, 1994.
  3. M. Eaddy et al. Do Crosscutting Concerns Cause Defects?. IEEE Trans. on Software Engineering, 34(4), pp. 497-515, 2008.
  4. E. Figueiredo et al. On the Maintainability of Aspect-Oriented Software: A Concern-Oriented Measurement Framework. In Proc. of the CSMR Conference. Athens, 2008.
  5. M. Fowler. Refactoring: Improving the Design of Existing Code. Addison-Wesley, Reading, MA, USA, 1999.
  6. J. Hannemann and G. Kiczales. Design Pattern Implementation in Java and AspectJ. In Proc. of the 17th OOPSLA Conference, pp. 161-173. Seattle, 2002.
  7. Dexter C. Kozen. The Design and Analysis of Algorithms. 1st edition, Springer, 1991.
  8. M. Lanza and R. Marinescu. Object-Oriented Metrics in Practice. Springer, 2006.
  9. R. Marinescu. Detection Strategies: Metrics-Based Rules for Detecting Design Flaws. Proc. of the Int’l. Conf. on Soft. Maintenance (ICSM), pp. 350-359, Chicago, 2004.
  10. C. Sant’anna et al. On the Modularity of Software Architectures: A Concern-Driven Measurement Framework. In Proc. of the 1st ECSA Conference. Spain, 2007.