An empirical study of untangling patterns of two-class dependency cycles

Qiong Feng, Shuwen Liu, Huan Ji, Xiaotian Ma, Peng Liang

TL;DR

This paper presents a large-scale empirical study of how software developers resolve "two-class" dependency cycles in the real world. By analyzing 38 open-source projects, the researchers identified five recurring successful untangling patterns and three common "counterintuitive" mistakes where developers accidentally shift a cycle rather than breaking it.

What Problem Does This Paper Address?

In software engineering, a dependency cycle occurs when two or more components depend on each other (e.g., Class A needs Class B, and Class B needs Class A). These cycles are notorious "maintenance killers": they make code harder to understand, break modularity, and complicate testing.

While many tools can detect these cycles, we know surprisingly little about how human developers actually fix them. Most automated refactoring research focuses on simple "local" fixes, but untangling a cycle often requires understanding the broader design context. The authors focus specifically on two-class cycles because they are the most frequent type and often represent the final, critical step in breaking down much larger, more complex circular dependencies.

Methods

The researchers developed a four-step pipeline to study cycle resolution:

  1. Detection: They used git history and a reverse engineering tool called Depends to identify dependency graphs before and after every commit in their dataset.
  2. Identification: They pinpointed "fix commits"—instances where a two-class cycle existed in version $N$ but vanished in version $N+1$.
  3. Manual Inspection: A team of researchers manually inspected hundreds of code snippets to understand the semantic changes. They didn't just look at the two cyclic classes; they traced the entire calling chain to see if the logic was moved, deleted, or abstracted.
  4. Pattern Synthesis: Using a decision tree strategy, they categorized the fixes into recurring "untangling patterns."

To validate if these patterns are predictable, they trained a Multi-layer Perceptron (MLP) classifier to see if the internal structure of a cycle (the types of dependencies like call, create, or extend) and its "design context" (how it interacts with neighbor classes) could predict which refactoring pattern a developer would choose.

Datasets

The study analyzed 38 high-quality open-source projects across diverse domains (data processing, databases, UI frameworks, etc.) from communities including:

  • Apache: Avro, Cassandra, HBase.
  • Google: Guava, Closure-compiler, ExoPlayer.
  • Facebook: Buck, Fresco, Litho.
  • Alibaba: Nacos, Druid, Sentinel.
  • Others: Javaparser, Stripe-java, Spoon.

In total, they identified 587 successful untangling cases and 69 unsuccessful (counterintuitive) cases.

Key Insights and Ideas

  • The "Dead Wood" Factor: The most common way cycles are resolved (40% of cases) is simply by removing unused or deprecated code. This suggests that many cycles are "ghosts" of old designs that were never cleaned up.
  • Feature Envy in Reverse: Many fixes involve moving code from one cyclic class to the other. This often happens because a method was placed in Class B but actually belonged in Class A (a "feature envy" code smell), creating an unnecessary back-reference.
  • Context is King: You cannot decide how to fix a cycle just by looking at the two classes involved. The researchers found that including the design context (how neighbor classes interact with the cycle) significantly improved the MLP model's ability to predict the correct refactoring pattern (F1-score increased from 0.52 to 0.64 for complex patterns).
  • The "Cycle Shift" Trap: About 10% of "fixes" are actually failures. Instead of breaking the cycle, the developer's change accidentally shifts the dependency to a parent class, a child class, or a third utility class, creating a new (and sometimes larger) cycle.

Key Results

The study identified five successful recurring patterns:

  1. Remove Unused/Deprecated Code (40.0%): Deleting the logic that caused the link.
  2. Move Code to a Third Class (23.4%): Shifting responsibility to a new or existing neutral class.
  3. Move Code Between Cyclic Files (14.8%): Realigning methods with the class they most closely relate to.
  4. Shorten Call Chain (12.1%): Bypassing an intermediate class to call a dependency directly.
  5. Leverage Built-In Features (1.0%): Replacing a custom dependency with a standard library feature (e.g., using Java Reflection instead of a direct class reference).

The MLP model achieved a high F1-score of 0.86 for the "Leverage Built-In Feature" pattern, proving that specific design traits strongly dictate certain solutions.

Limitations and Future Work

  • Language Specificity: The study focused exclusively on Java. Dependency patterns might differ in languages with different module systems (like C++ or Go).
  • Single-Commit Focus: The methodology assumes a cycle is fixed in a single commit. In reality, large architectural refactorings might span multiple commits, which this study might miss.
  • Automation: While the paper shows patterns are predictable, it doesn't provide a tool to automatically perform the refactoring. The authors suggest that building an IDE plugin (similar to JDeodorant) to recommend these specific patterns is the logical next step.

Summary updated

BibTeX
@article{feng2024empirical,
  author = {Qiong Feng and Shuwen Liu and Huan Ji and Xiaotian Ma and Peng Liang},
  title = {{An empirical study of untangling patterns of two-class dependency cycles}},
  journal = {Empir. Softw. Eng.},
  year = {2024},
  doi = {10.1007/s10664-023-10438-0},
}
Figure