Page 339 - 《软件学报》2025年第8期
P. 339
3762 软件学报 2025 年第 36 卷第 8 期
过程中的回代步骤. ChatGPT 生成的注释则包含补充性的信息“using LU decomposition”, 即使用了 LU 分解的方
法, 这是由代码的实现逻辑总结出的正确信息, 开发者无需深入阅读代码, 便可通过注释获得这一信息. 在例 2 中,
ChatGPT 生成的注释内容同样更加丰富, 覆盖了人工参考注释与 CodeT5 生成注释的内容——缩减数组的容量,
同时涵盖了代码中的重要内容“using linear probing to rehash the elements”, 即通过线性探测法对元素重新哈希处
理. 如果开发者只阅读方法签名, 则无法获得这一信息.
CodeT5
40
ChatGPT
35
35 30
30
Probability (%) 25 25
20
20
15
10 15
10
5
5
0
0 1 2 3 4 5 6 7 8 9 10
mesia CodeT5 ChatGPT
图 12 CodeT5 与 ChatGPT 生成注释在 mesia 指标上 图 13 CodeT5 与 ChatGPT 生成注释的长度分布
的分布
private ColumnVector _backSubstitution (final ColumnVector y) private void shrink() {
throws MatrixException { int oldCapacity = data.length;
final ColumnVector x = new ColumnVector (m_nRows); if (oldCapacity == 1)
for (int r = m_nRows - 1; r >= 0; - - r) { return;
final int pr = m_aPermutation[r]; E[] oldData = data;
float dot = 0; int newCapacity = oldCapacity >> 1;
for (int c = r + 1; c < m_nRows; ++c) { E[] newData = (E[]) new Object[newCapacity];
dot += m_aLU.at (pr, c) * x.at (c); for (int i = 0; i < oldCapacity; i++) {
} E e = oldData[i];
x.set (r, (y.at (r) -dot) / m_aLU.at (pr, r)); if (e != null)
} LinearProbing.add(newData, e);
return x; }
} this.data = newData;
}
人工参考注释: Solve Ux = y for x by back substitution. 人工参考注释: Decreasing the capacity of the table.
CodeT5生成注释: Back substitution. CodeT5生成注释: Shrink the data array to a new capacity.
ChatGPT生成注释:This function performs back substitution ChatGPT生成注释:This function shrinks the data array by
to solve a system of linear equations using half its capacity, using linear probing
LU decomposition. to rehash the elements.
(a) 例1 (b) 例2
图 14 ChatGPT 和 CodeT5 生成注释与人工参考注释的对比案例
综上结果表明, ChatGPT 生成注释的内容更为丰富, 相对于方法签名, 会补充更多的信息, 这些信息能够帮助
开发者理解代码. 但是, 研究过程中也发现 ChatGPT 生成的注释倾向于对代码功能进行详细的描述, 涉及的代码
细节可能较多, 导致注释冗长, 使阅读注释的开发者难以把握重点内容. 当然, 本文目前使用的 prompt 较为简单,
未来可以尝试其他的 prompt, 从而控制代码注释的生成, 使其更符合开发者的需求.
6.2 发 现
相对于传统模型, 以 ChatGPT 为代表的大语言模型生成的代码注释内容丰富、有用性更强, 有利于开发者更
深入地理解代码, 但存在冗长不便阅读的问题. 未来代码注释的评价更应该根据开发者的意图和具体场景进行具
有针对性的评价.
7 有效性分析
本文选用 4 个维度 8 个指标对开放数据集中的代码注释、典型方法的生成注释和 ChatGPT 生成的代码注释

