Code Best Practices
Comments
- •No Trivial Comments: Strictly avoid commenting on what the code does if the syntax makes it obvious.
- •Forbidden:
count++ // increment count - •Forbidden:
if err != nil { // check error
- •Forbidden:
- •Intent Over Implementation: Use comments only to explain why a specific decision was made or to clarify complex business logic.
Naming Conventions
- •Self-Documenting Code: Choose method and variable names that explain their purpose without needing a comment.
- •Specific over Generic:
- •
fetchUserData(userID string)instead ofgetData(id string) - •
invoiceProcessingQueueinstead ofqueueorq
- •
- •Boolean Variables: Should sound like questions (e.g.,
isValid,hasAccess,fileHere).
Philosophy
- •Code is read much more often than it is written.
- •If you feel the need to write a comment to explain what a block does, consider refactoring the code into a named function instead.