Back to all reviewers

Explicit nil handling

vitessio/vitess
Based on 4 comments
Go

Always handle nil values explicitly in your code to improve clarity and prevent subtle bugs. When a function needs to deal with potentially nil values:

Null Handling Go

Reviewer Prompt

Always handle nil values explicitly in your code to improve clarity and prevent subtle bugs. When a function needs to deal with potentially nil values:

  1. Document the expected behavior when nil values are encountered
  2. Use early returns to handle nil cases at the beginning of functions
  3. Avoid silent fall-throughs in switch statements when handling nil
  4. Know when nil checks are necessary versus when Go handles nil cases automatically

For example, instead of using a switch statement with a fall-through for nil:

func AppendGTIDInPlace(rp Position, gtid GTID) Position {
  switch {
  case gtid == nil:
  case rp.GTIDSet == nil:
    rp.GTIDSet = gtid.GTIDSet()
  default:
    // Handle non-nil case
  }
  // ...
}

Prefer explicit handling with early returns:

func AppendGTIDInPlace(rp Position, gtid GTID) Position {
  // If gtid is nil, treat it as a no-op and return the input Position.
  if gtid == nil {
    return rp
  }
  if rp.GTIDSet == nil {
    rp.GTIDSet = gtid.GTIDSet()
  } else {
    // Handle non-nil case
  }
  // ...
}

Also, remember that Go has built-in nil handling for some operations. For instance, append works safely with nil slices:

// This is unnecessary
if req.FilterRules != nil {
  bls.Filter.Rules = append(bls.Filter.Rules, req.FilterRules...)
}

// This is sufficient
bls.Filter.Rules = append(bls.Filter.Rules, req.FilterRules...)

This approach makes the intention clear, improves code readability, and prevents unexpected behavior.

4
Comments Analyzed
Go
Primary Language
Null Handling
Category

Source Discussions