Abstract
There has been a long debate on which programming language can help write better object-oriented programs. However, to date little response is given to this issue with empirical evidence. In this paper, we perform a comparative study on C++, C#, and Java programs by using object-oriented metrics, which comprise measures for class size, complexity, coupling, cohesion, inheritance, encapsulation, polymorphism, and reusability. Our experiment is conducted on 78 tasks in Rosetta Code, a code repository providing solutions to the same programming tasks in different languages. The experimental results show that: (1) C++ classes are significantly larger than C# and Java classes in size, but their complexity does not differ significantly; (2) C# classes are significantly more likely to be coupled than C++ and Java classes through inter-class method invocations instead of direct data access; (3) C# and Java classes tend to be more cohesive than C++ classes; (4) C# and Java significantly outperform C++ in building deep inheritance trees; and (5) programs written in C++, C#, and Java do not show a significant difference in class encapsulation, polymorphism, and reusability.
Introduction
Software engineers have argued for decades about the merits of different Object-Oriented (OO) languages. Is Java "more" object-oriented than C++? Does C# lead to cleaner code? Most of these arguments rely on intuition or anecdotal experience. This study addresses the lack of empirical data by conducting a head-to-head comparison of C++, C#, and Java, aiming to determine if language choice fundamentally changes the structural quality of software in terms of class size, complexity, coupling, cohesion, and inheritance.
Methodology
The researchers utilized Rosetta Code, a programming chrestomathy repository, to ensure a fair comparison. By selecting 78 specific tasks implemented in all three languages, the study minimizes "noise" that would come from comparing disparate application domains (e.g., a physics engine vs. a web server).
Metrics Suite
The study employed 23 standardized OO metrics, including the Chidamber & Kemerer (CK) metrics for class-level analysis and Metrics for Object-Oriented Design (MOOD) for system-level analysis. Data was collected using the "Understand" code analysis tool and custom scripts to calculate complex metrics like Class Complexity (CC).
Statistical Analysis
To ensure robustness, the researchers used the Wilcoxon signed-rank test for significance ($p < 0.05$), Cliff’s $\delta$ for effect size, and a signed ratio ($R$) to compare medians across the 78 tasks.
Key Insights and Results
The analysis revealed several distinct structural patterns across the three languages:
Size and Complexity
C++ classes were found to be significantly larger than their C# and Java counterparts. The median Number of Methods (NOM) in C++ was over 2x larger than in C#. The authors hypothesize a "procedural hangover" where C++ developers, working in a multi-paradigm language, often cram more functionality into single classes rather than distributing it across a hierarchy. Interestingly, despite the size difference, overall complexity (CC) did not show a significant variance.
Inheritance and Cohesion
C# and Java significantly outperformed C++ in building deep inheritance trees (DIT). This is likely due to the unified root (Object class) in C# and Java, which naturally encourages hierarchical structures. Furthermore, C# and Java classes were consistently more cohesive (LCOM), meaning internal methods were more likely to operate on shared data attributes.
Coupling and Encapsulation
C# exhibited higher coupling via method invocations (RFC) but lower coupling via direct data access (DAC). This is attributed to C# "Properties," which appear as method calls to analysis tools but provide cleaner syntax for developers. Notably, the study found no statistical difference in Encapsulation, Polymorphism, or Reusability, suggesting these traits depend more on developer skill than language syntax.
Statistical Evidence
The following table summarizes the comparative statistical results that support these findings, highlighting the p-values and effect sizes for each metric category.
Conclusion
While C++, C#, and Java all support object-oriented principles, the choice of language influences the structural characteristics of the resulting code. C# and Java encourage more cohesive and hierarchical designs, whereas C++ tends toward larger, more procedural-style classes. However, core OO benefits like encapsulation and reusability remain largely language-independent, highlighting the continued importance of developer expertise in building high-quality systems.
Summary updated