Declare variables close to their usage point and choose appropriate types to improve code readability and reduce cognitive load. Avoid declaring variables at the top of functions when they're only used once later in the code. Remove unused variables and imports to keep the codebase clean.
Declare variables close to their usage point and choose appropriate types to improve code readability and reduce cognitive load. Avoid declaring variables at the top of functions when they’re only used once later in the code. Remove unused variables and imports to keep the codebase clean.
Key practices:
Example of improvement:
// Instead of declaring at top:
func updateConfigFromParameter(config configuration.Configuration, args []string, rawArgs []string) {
doubleDashArgs := []string{}
doubleDashPosition := -1 // could be boolean
// ... later usage
}
// Declare where used with appropriate type:
func updateConfigFromParameter(config configuration.Configuration, args []string, rawArgs []string) {
// ... other code
doubleDashArgs := []string{}
foundDoubleDash := false // boolean is clearer than position integer
// ... immediate usage
}
This practice reduces the mental overhead of tracking variable scope and purpose, making code easier to understand and maintain.
Enter the URL of a public GitHub repository