代码注释约定
所有过程的开始部分都应有描述其功能的简要注释。这些注释并不描述细节信息(如何实现功能),这是因为细节有时要频繁更改。这样就可以避免不必要的注释维护工作以及错误的注释。细节信息由代码本身及必要的内部注释来描述。
当传递给过程的参数的用途不明显,或过程对参数的取值范围有要求时,应加以说明。如果过程改变了函数和变量的返回值(特别是通过参数引用来改变),也应在过程的开始部分描述该返回值。
过程开始部分的注释应包含以下区段标题。相关样例,请参阅后面的“格式化代码”部分。
| 区段标题 | 注释内容 |
|---|---|
| 目的 | 过程的功能(不是实现功能的方法)。 |
| 假设 | 其状态影响此过程的外部变量、控件或其他元素的列表。 |
| 效果 | 过程对每个外部变量、控件或其他元素的影响效果的列表。 |
| 输入 | 每个目的不明显的参数的解释。每个参数都应占据单独一行并有其内部注释。 |
| 返回 | 返回值的解释。 |
请记住以下几点:
- 每个重要的变量声明都应有内部注释,描述变量的用途。 应清楚地命名变量、控件和过程,仅在说明复杂细节时需要内部注释。 应在脚本的开始部分包含描述该脚本的概述,列举对象、过程、运算法则、对话框和其他系统从属物。有时一段描述运算法则的假码是很有用的。
格式化代码
应尽可能多地保留屏幕空间,但仍允许用代码格式反映逻辑结构和嵌套。以下为几点提示:
- 标准嵌套块应缩进 4 个空格。 过程的概述注释应缩进 1 个空格。 概述注释后的最高层语句应缩进 4 个空格,每一层嵌套块再缩进 4 个空格。例如:
'*********************************************************' Purpose: Locates the first occurrence of a specified user ' in the UserList array.' Inputs: strUserList(): the list of users to be searched.' strTargetUser: the name of the user to search for.' Returns: The index of the first occurrence of the strTargetUser ' in the strUserList array. ' If the target user is not found, return -1.'*********************************************************Function intFindUser (strUserList(), strTargetUser) Dim i ' Loop counter. Dim blnFound ' Target found flag intFindUser = -1 i = 0 ' Initialize loop counter Do While i <= Ubound(strUserList) and Not blnFound If strUserList(i) = strTargetUser Then blnFound = True ' Set flag to True intFindUser = i ' Set return value to loop count End If i = i + 1 ' Increment loop counter LoopEnd Function







