Structure code to maximize readability and reduce cognitive load through proper formatting and control flow patterns. This includes breaking long function signatures across multiple lines, using early returns to reduce nesting levels, and extracting complex boolean expressions into well-named functions.
Structure code to maximize readability and reduce cognitive load through proper formatting and control flow patterns. This includes breaking long function signatures across multiple lines, using early returns to reduce nesting levels, and extracting complex boolean expressions into well-named functions.
Key practices:
Example of improved readability through early returns:
// Instead of:
if t, ok := activeTargets[target.labels.Hash()]; ok {
// ... log here ...
}
// Prefer:
t, ok := activeTargets[target.labels.Hash()]
if !ok {
continue
}
// ... log here ...
Example of breaking long function signatures:
func NewZookeeperTreeCache(
conn *zk.Conn, path string,
events chan ZookeeperTreeCacheEvent,
logger *slog.Logger,
failureCounter prometheus.Counter,
numWatchers prometheus.Gauge,
) *ZookeeperTreeCache {
These practices make code easier to understand, debug, and maintain by reducing visual complexity and making the logical flow more apparent.
Enter the URL of a public GitHub repository