一句话:研究 231 名新手对代码风格与可读性的判断,发现可读性偏好与代码理解相分离,并能预测其编码选择。
新手写出的功能正确代码常冗余、啰嗦或不地道,但原因不明:是语言理解肤浅,还是不熟悉专家风格?论文让 231 名新手对覆盖七个主题的代码片段进行风格与可读性评判;每组片段功能相同,分别采用常见新手写法或更优雅的专家写法。结果显示,76% 的新手认为专家片段风格最好,仅 64% 认为其最可读;但无论可读性偏好如何,两组片段的理解表现相近。在一个主题的代码编写任务中,可读性偏好还能预测学生的编写模式。作者认为,偏好新手写法未必意味着深刻误解,而更多反映写作选择;轻量工具可提示常见模式与专家解法,并帮助学生认识专家解法更可读。
Novices' functionally-correct code is often redundant, verbose, or un-idiomatic. Such code could indicate shallow understanding of the programming language, or unfamiliarity with experts' preferences for code structure. Understanding why novices write poorly is important for designing instruction and tools to help novices write elegantly. 231 novices judged style and readability for sets of code snippets targeting seven topics. Within each set, functionality was the same, but the writing followed either common novice patterns or a more elegant, "expert" pattern. Overall, 76% of novices thought the "expert" snippets had the best style, but only 64% said those snippets were most readable. However, comprehension was similar for both "expert" and novice patterns, regardless of readability preferences. This suggests that students who prefer novice patterns do not necessarily have deep misunderstandings about the programming language. One topic included a code-writing task, and students' readability preferences were predictive of their code-writing patterns, suggesting that readability preferences reflect writing choices rather than comprehension. Thus, novices may benefit from lightweight tools that identify common patterns and suggest an "expert" solution, while helping them see that the "expert" solution is more readable than they think.