Back to all reviewers

Validate loop boundary conditions

oven-sh/bun
Based on 2 comments
Other

When implementing iterative algorithms, ensure comprehensive handling of edge cases and boundary conditions. This includes: 1. Using while loops instead of if-else chains for repetitive operations

Algorithms Other

Reviewer Prompt

When implementing iterative algorithms, ensure comprehensive handling of edge cases and boundary conditions. This includes:

  1. Using while loops instead of if-else chains for repetitive operations
  2. Explicitly handling overflow/underflow scenarios
  3. Validating both start and end conditions
  4. Testing with boundary values

Example - Before (problematic):

if (strings.endsWith(normalized_name, "/")) {
    normalized_name = normalized_name[0 .. normalized_name.len - 1];
} else if (strings.endsWith(normalized_name, "\\")) {
    normalized_name = normalized_name[0 .. normalized_name.len - 1];
}

Example - After (robust):

while (strings.endsWith(normalized_name, "/") or 
       strings.endsWith(normalized_name, "\\")) {
    normalized_name = normalized_name[0 .. normalized_name.len - 1];
}

This approach prevents issues like:

  • Missed cases in chained conditions
  • Integer overflow/underflow in calculations
  • Incomplete processing of repeated patterns
  • Boundary condition errors

When implementing iterative algorithms, always consider:

  1. What happens at zero/empty input?
  2. What happens at maximum values?
  3. Can the operation need to be repeated?
  4. Are all edge cases handled explicitly?
2
Comments Analyzed
Other
Primary Language
Algorithms
Category

Source Discussions