Ensure code remains readable and maintainable by following these practices: 1. **Combine case statements with identical outcomes** to reduce duplication. When multiple cases have identical code blocks, group them together:
Ensure code remains readable and maintainable by following these practices:
// GOOD
switch planType {
case tsm1.PT_Standard, tsm1.PT_SmartOptimize:
return common
case tsm1.PT_NoOptimize:
return testLevelResults{}
}
// AVOID
switch planType {
case tsm1.PT_Standard:
return common
case tsm1.PT_SmartOptimize:
return common
case tsm1.PT_NoOptimize:
return testLevelResults{}
}
// GOOD
common := testLevelResults{
level4Groups: []tsm1.PlannedCompactionGroup{
{
tsm1.CompactionGroup{"01-05.tsm", "02-05.tsm", "03-05.tsm", "04-04.tsm"},
tsdb.DefaultMaxPointsPerBlock,
},
},
}
switch planType {
case tsm1.PT_Standard, tsm1.PT_SmartOptimize, tsm1.PT_NoOptimize:
return common
}
// GOOD
const AggressiveMaxPointsPerBlock = DefaultMaxPointsPerBlock * 100
// AVOID
e.Compactor.Size = tsdb.DefaultMaxPointsPerBlock * 100
// GOOD
var loaded bool
if f, loaded = m.fields.LoadOrStore(newField.Name, newField); f.Type != typ {
return f, !loaded, ErrFieldTypeConflict
}
return f, !loaded, nil
// AVOID
if f, loaded := m.fields.LoadOrStore(newField.Name, newField); loaded {
if f.Type != typ {
return f, false, ErrFieldTypeConflict
}
return f, false, nil
} else {
return f, true, nil
}
These practices make code easier to understand, maintain, and reduce bugs from copy-paste errors or inconsistencies.
Enter the URL of a public GitHub repository