7 examples
Off-by-one error
Calculation errors from loop or array indexes off by one.
[ FAQ1 ]
What is an off-by-one error?
An off-by-one error occurs when calculations, indexing, or loop bounds are off by a single value, typically due to incorrect logic involving array indexes or iteration conditions. These mistakes are often called fencepost errors, referring to the subtle boundary miscalculations common in loops or ranges. Off-by-one errors produce subtle bugs, such as accessing the wrong array elements, missing the last item in a loop, or iterating one step too far, often causing application logic issues, data inaccuracies, or runtime exceptions.
[ FAQ2 ]
How to fix off-by-one errors
To fix off-by-one errors, carefully verify loop conditions, array indexes, and boundary conditions, explicitly checking whether loop ranges start and end exactly as intended. Clearly document and review indexing logic, emphasizing zero-based indexing conventions typically used in programming languages. Use debugging tools and tests to systematically confirm correctness at loop boundaries or index operations, focusing explicitly on edge cases. Regular code reviews, automated testing, and clear logic structuring significantly reduce the occurrence of off-by-one mistakes.
diff block
greptile
logic: Adding 1 to days when days < 7 but not when days < 1 creates inconsistent behavior. This could cause off-by-one errors in date displays.
diff block
greptile
logic: potential off-by-one error in slice operation when range[0] is 0
suggested fix
const displayedProjects = range
+ ? sortedProjects.slice(Math.max(0, range[0] - 1), range[1] ?? sortedProjects.length)
: sortedProjects;
diff block
greptile
style: Consider using range-based iteration instead of index-based for cleaner code and to avoid potential off-by-one errors.
diff block
greptile
Off-by-one errors are a common programming mistake where a loop or array access is performed one time too many or one time too few. They typically happen when:
1. Using incorrect comparison operators (< vs <=)
2. Starting loops at the wrong index (0 vs 1)
3. Miscalculating array boundaries
In your code, you're correctly using `i < arrLength` which avoids an off-by-one error (if you had used `i <= arrLength`, you would access an element outside the array bounds).
For cleaner code, you could use a range-based loop instead of an index-based one. This eliminates the need to manually track indices and reduces the chance of off-by-one errors:
This approach is more idiomatic in Go and less error-prone since the range loop automatically handles the iteration boundaries for you.
suggested fix
func sequentialSearch(arrPtr *[]int, target int) bool {
+ for _, value := range *arrPtr {
+ if value == target {
return true
}
}
return false
}
diff block
greptile
logic: entries.count() includes the file descriptor used to read the directory itself, leading to an off-by-one error. Consider filtering out the current fd or subtracting 1 from the final count.
Want to avoid this bug in your codebase? Try Greptile.
Avoid this bug!