記錄你的工作
記錄你的工作以供以後使用是一種很好的做法,尤其是在編寫動態工作負載時。好的評論應該解釋為什麼程式碼在做某事,而不是程式碼在做什麼。
Function Bonus(EmployeeTitle as String) as Double
If EmployeeTitle = "Sales" Then
Bonus = 0 'Sales representatives receive commission instead of a bonus
Else
Bonus = .10
End If
End Function
如果你的程式碼如此模糊以至於需要註釋來解釋它正在做什麼,請考慮將其重寫為更清晰,而不是通過註釋來解釋它。例如,而不是:
Sub CopySalesNumbers
Dim IncludeWeekends as Boolean
'Boolean values can be evaluated as an integer, -1 for True, 0 for False.
'This is used here to adjust the range from 5 to 7 rows if including weekends.
Range("A1:A" & 5 - (IncludeWeekends * 2)).Copy
Range("B1").PasteSpecial
End Sub
澄清程式碼更容易遵循,例如:
Sub CopySalesNumbers
Dim IncludeWeekends as Boolean
Dim DaysinWeek as Integer
If IncludeWeekends Then
DaysinWeek = 7
Else
DaysinWeek = 5
End If
Range("A1:A" & DaysinWeek).Copy
Range("B1").PasteSpecial
End Sub