Back to all reviewers

Maintain code readability

influxdata/influxdb
Based on 5 comments
Go

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:

Code Style Go

Reviewer Prompt

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:
// 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{}
}
  1. Extract common values to avoid repetition, especially in maps or switch statements:
// 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
}
  1. Use named constants instead of magic numbers. Operations between constants should also be assigned to named constants:
// GOOD
const AggressiveMaxPointsPerBlock = DefaultMaxPointsPerBlock * 100

// AVOID
e.Compactor.Size = tsdb.DefaultMaxPointsPerBlock * 100
  1. Simplify nested conditionals where possible:
// 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.

5
Comments Analyzed
Go
Primary Language
Code Style
Category

Source Discussions