Skip to content

Off-by-One Errors: The Small Counting Bugs Behind Broken Pagination, Dates, and Reports

Java logo above the question What Is ++ in Java.

A blog archive shows the same post at the bottom of page one and the top of page two. A booking system lets customers reserve a room for one night too many. A monthly report leaves out the last day of the month. These are off-by-one errors, one of the most common kinds of software bug, and they often come down to a single character in the code.

This guide explains them for business owners and project managers. It starts with the Java increment operator ++, which appears in most counting code, and explains why counting from zero causes confusion. It then walks through the places off-by-one errors show up on websites and how to test for them.

What ++ does

In Java and many similar languages, ++ adds one to a number. count++ is shorthand for count = count + 1. It appears everywhere counting happens: looping through the products on a page, numbering invoice lines, counting failed login attempts, or moving to the next page of results.

There are two forms. With count++, the old value is used first and then increased. With ++count, the value is increased first and the new value is used. On a line of its own the difference does not matter, but inside a larger expression it changes the result by one, which is exactly the kind of detail that produces off-by-one bugs.

Why counting from zero trips people up

Most programming languages, including Java and JavaScript, number the items in a list starting from zero. A list of ten products has positions zero to nine. A loop written as "start at zero and continue while the position is less than ten" handles all ten. Change "less than" to "less than or equal to" and the code tries to read an eleventh item that does not exist, which in Java causes an error.

The opposite mistake skips the first or last item quietly. Humans count from one and programs count from zero, and every boundary between the two, such as page numbers shown to visitors, is a place where one can slip.

CorrectPage 1: items 1 to 10Page 2: items 11 to 20OverlapPage 1: items 1 to 10Page 2: items 10 to 19GapPage 1: items 1 to 10Page 2: items 12 to 21One wrong boundary repeats an item or hides one
Pagination is the most visible place an off-by-one error appears.

Where off-by-one errors show up on websites

Pagination is the classic case: duplicated or missing items between pages, a "next" link on the last page that leads to an empty page, or a count that says "showing 1 to 11 of 10". Search engines can also find and index those empty or duplicated pages, which wastes crawling on pages with nothing useful. Our guide to search engine optimization explains why clean, consistent pages matter.

Dates are the second big source. Is a booking from the 10th to the 12th two nights or three days? Does a report "for March" include the 31st up to midnight? Does a promotion that ends on Sunday stop at the start of Sunday or the end? Each of these depends on whether a range includes its last value, and the code must match what the business means.

Limits, stock, and counters

Off-by-one errors also affect limits. A rule meant to lock an account after five failed logins may lock it after four or six. A "last item in stock" message may appear when two remain or after the final one sells. A free shipping threshold of $50 may apply at $50.01 but not at $50.00 because the code used "greater than" instead of "greater than or equal to".

Write limits in plain words with an example, for example "free shipping applies to orders of $50.00 or more, so a $50.00 order qualifies". That sentence removes the ambiguity before a developer has to guess.

Reports and analytics

Reports that sum data by day, week, or month are prone to boundary mistakes, especially around time zones and daylight saving changes. A sale made at 11:30 p.m. local time may fall into the next day in a report that uses a different time zone. If your store report and your analytics never agree for the first and last day of a period, check which time zone and which boundary each uses.

How to test for them

  • Test exactly at each boundary: the first and last item, the first and last page, the limit itself, and one either side.
  • Check date ranges at the start and end of a day, a month, and a daylight saving change.
  • Compare totals shown to visitors with the actual number of records.
  • Write the expected result for each test before running it.

Developers call this boundary testing. Ask whether your project's automated tests include it for pagination, dates, and limits.

Writing requirements that prevent them

Many off-by-one bugs are really unclear requirements. "Show the last 30 days" could include today or not. "Allow up to three bookings" could mean three or fewer, or fewer than three. When you brief a developer, give a concrete example for every range and limit, including the edge case. It takes minutes and saves debugging time later.

Spreadsheets have the same problem

Off-by-one errors are not only a developer's problem. In a spreadsheet, a formula that sums rows 2 to 30 instead of 2 to 31 misses a day of sales, and a range that starts at the header row counts one item too many. Our guide to COUNTIF in Google Sheets shows how to check counts against the real number of rows. When software is involved, a test engineer makes boundary checks part of every release; our guide to hiring test automation engineers explains the role.

Small bugs, easy fixes

The ++ operator adds one to a number and drives most of the counting in Java code. Because programs count from zero and people count from one, boundaries are easy to get wrong. The results include repeated or missing items in pagination, wrong date ranges, limits that trigger too early or late, and reports that disagree.

Clear requirements with examples and boundary tests catch most of these before customers do. If your site's pagination or reports look inconsistent, our web development team can find the cause, and our guide to rounding errors in online stores covers another small bug with real costs.

Want this checked on your own site?

Check one URL in Swiss Knife SEO without an account. A free account supports crawls of up to 100 URLs.