Documentation comments should be accurate, concise, and add genuine value rather than restating obvious code behavior. Focus on explaining "what" the code does and "why" it exists, not "how" it works step-by-step.
Documentation comments should be accurate, concise, and add genuine value rather than restating obvious code behavior. Focus on explaining “what” the code does and “why” it exists, not “how” it works step-by-step.
Key principles:
Example of good documentation:
// addTypeAndUnitLabels appends type and unit labels to the given labels slice.
func addTypeAndUnitLabels(labels []prompb.Label, metricType, unit string) []prompb.Label {
// implementation
}
Example of poor documentation:
// addTypeAndUnitLabels appends type and unit labels to the given labels slice if the setting is enabled.
// This function iterates through the labels and adds new ones based on the parameters provided.
func addTypeAndUnitLabels(labels []prompb.Label, metricType, unit string) []prompb.Label {
// implementation that doesn't actually check any setting
}
Assume readers are proficient in Go and focus documentation on the purpose and behavior rather than implementation details.
Enter the URL of a public GitHub repository