Table of Contents
Fetching ...

Improving Code Comprehension through Cognitive-Load Aware Automated Refactoring for Novice Programmers

Subarna Saha, Alif Al Hasan, Fariha Tanjim Shifat, Mia Mohammad Imran

Abstract

Novice programmers often struggle to comprehend code due to vague naming, deep nesting, and poor structural organization. While explanations may offer partial support, they typically do not restructure the code itself. We propose code refactoring as cognitive scaffolding, where cognitively guided refactoring automatically restructures code to improve clarity. We operationalize this in CDDRefactorER, an automated approach grounded in Cognitive-Driven Development that constrains transformations to reduce control-flow complexity while preserving behavior and structural similarity. We evaluate CDDRefactorER using two benchmark datasets (MBPP and APPS) against two models (gpt-5-nano and kimi-k2), and a controlled human-subject study with novice programmers. Across datasets and models, CDDRefactorER reduces refactoring failures by 54-71% and substantially lowers the likelihood of increased Cyclomatic and Cognitive complexity during refactoring, compared to unconstrained prompting. Results from the human study show consistent improvements in novice code comprehension, with function identification increasing by 31.3% and structural readability by 22.0%. The findings suggest that cognitively guided refactoring offers a practical and effective mechanism for enhancing novice code comprehension.

Improving Code Comprehension through Cognitive-Load Aware Automated Refactoring for Novice Programmers

Abstract

Novice programmers often struggle to comprehend code due to vague naming, deep nesting, and poor structural organization. While explanations may offer partial support, they typically do not restructure the code itself. We propose code refactoring as cognitive scaffolding, where cognitively guided refactoring automatically restructures code to improve clarity. We operationalize this in CDDRefactorER, an automated approach grounded in Cognitive-Driven Development that constrains transformations to reduce control-flow complexity while preserving behavior and structural similarity. We evaluate CDDRefactorER using two benchmark datasets (MBPP and APPS) against two models (gpt-5-nano and kimi-k2), and a controlled human-subject study with novice programmers. Across datasets and models, CDDRefactorER reduces refactoring failures by 54-71% and substantially lowers the likelihood of increased Cyclomatic and Cognitive complexity during refactoring, compared to unconstrained prompting. Results from the human study show consistent improvements in novice code comprehension, with function identification increasing by 31.3% and structural readability by 22.0%. The findings suggest that cognitively guided refactoring offers a practical and effective mechanism for enhancing novice code comprehension.
Paper Structure (27 sections, 5 figures, 4 tables)

This paper contains 27 sections, 5 figures, 4 tables.

Figures (5)

  • Figure 1: Examples from Reddit where novice programmers talking about difficulties in understanding other's code
  • Figure 2: Overview of the Methodology.
  • Figure 3: CodeBLEU similarity distributions after refactoring on MBPP (top) and APPS (bottom).
  • Figure 4: Original code (top), erroneous baseline refactoring (middle), and correct CDDRefactorER refactoring (bottom).
  • Figure 5: Original code (CC=4, CogC=4) (top), baseline refactoring (CC=2, CogC=1) (middle), and CDDRefactorER refactoring with lowest complexity (CC=1, CogC=0) (bottom).