Year: 2026
Type: dissertation
Abstract: The thesis addresses the problem of automatically recognizing design patterns within source code, with particular attention to code generated by Large Language Models (LLMs). In the current context of software development, characterized by increasingly complex and modular systems, design patterns represent fundamental tools for ensuring code quality and maintainability. However, the automatic identification of such patterns remains an open challenge, mainly due to the high variability of real-world implementations compared to their canonical definitions. Traditionally, design pattern detection has been approached through structural code analysis, heuristic techniques, and graph matching methods. Although these approaches offer a high degree of interpretability, they are often rigid and not robust to implementation variability. On the other hand, machine learning and deep learning methods provide greater generalization capabilities, but introduce issues related to the need for large annotated datasets and limited model interpretability. In this context, the thesis proposes a hybrid approach that combines structural analysis with semantic representations of code. The goal is to leverage the strengths of both perspectives: on the one hand, the precision and transparency of structural rules; on the other, the ability of embeddings to capture deep semantic relationships that are invariant to syntactic variations. The architecture of the developed system is organized as a multi-stage analysis pipeline. First, the source code is preprocessed using identifier anonymization techniques, with the aim of reducing lexical bias and forcing the model to focus on structural and semantic aspects rather than variable names. Subsequently, structural features are extracted from the Abstract Syntax Tree (AST), which represents the syntactic structure of the code. The core of the system consists of two main modules: the Structural Score and the Alignment Score. The Structural Score module evaluates how well the analyzed code satisfies the structural constraints of design patterns. This process is based on a taxonomy of evidence organized into four pillars, including aspects such as class relationships, inheritance hierarchies, and object interaction patterns. The evidence is aggregated using a geometric mean, producing an interpretable score that reflects the degree of structural compliance of the code with respect to a given pattern. The Alignment Score module, on the other hand, relies on vector representations of code (embeddings) generated using CodeBERT, a Transformer-based neural model trained on source code and natural language. These embeddings map code fragments into a continuous latent space, where geometric proximity between vectors reflects semantic similarity. Within this space, the analyzed code is compared with prototypical representations of different design patterns, allowing the estimation of semantic alignment. To improve the quality of the latent space, the model is further optimized using Supervised Contrastive Learning techniques. This approach reduces intra-class variance and increases separation between different classes, making the representations more discriminative. In particular, contrastive learning addresses the issue of embedding anisotropy, which tends to compress vectors into narrow regions of the space, thereby limiting the ability to distinguish between different patterns. The two scores, Structural Score and Alignment Score, are finally combined using a late fusion strategy based on a geometric mean. This enables the integration of structural and semantic information into a more robust final decision, leveraging the complementarity of the two approaches. In cases of ambiguity, a lexicographic ordering strategy is applied to determine the most likely pattern. The experimental evaluation was conducted using both synthetic datasets generated via LLMs and a manually validated Gold Standard Test Set. The evaluation metrics include precision, recall, and F1-score for classification, as well as metrics specific to latent space analysis, such as Silhouette Score, Mean Alignment, and Uniformity. The results show that the proposed hybrid approach achieves competitive performance, demonstrating greater robustness compared to purely structural or purely neural approaches. In particular, the system shows a good ability to distinguish between complex patterns, even in the presence of implementation variability. However, some difficulties emerge in discriminating structurally similar patterns, such as Adapter and Strategy, highlighting the intrinsic limitations of the problem. The ablation study further confirms the significant contribution of the different system components, particularly the contrastive fine-tuning of embeddings, which substantially improves class separability in the latent space. Finally, the thesis discusses the main limitations of the work. Among these are the restriction to single-file analysis, which prevents capturing cross-file relationships, and the difficulty in recognizing highly atypical pattern implementations. Moreover, the use of automatically generated datasets, while advantageous in terms of scalability, may introduce biases related to generative models. Possible future directions include extending the analysis to multi-file systems, introducing continuous structural scores, and adapting the framework to different programming languages. In conclusion, the work demonstrates how a hybrid approach combining structural analysis and semantic representations constitutes a promising solution to the problem of design pattern detection, opening new perspectives for the application of advanced machine learning techniques in the domain of software engineering.
Author: FRANCO, ARCANGELO
Language: Italian
Subfield: Information Systems
Field: Computer Science
Domain: Physical Sciences
Open Access status: green
OpenAlex ID: W7159562114
