Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Software Design & Development · Grade 6-8

Edge Cases

Edge cases are the inputs at the limits of what a program is meant to handle, where code that works on ordinary inputs often breaks: an empty list, zero, a single item, the largest allowed value, a missing field.

x∈{∅,0,min,max}

Quick Answer

Edge cases are the inputs at the limits of what a program is meant to handle, where code that works on ordinary inputs often breaks: an empty list, zero, a single item, the largest allowed value, a missing field. Use them whenever you test or design a function: once the normal case passes, try the smallest, the largest, the empty and the 'exactly on the line' inputs, and decide the correct output for each. A valid edge case has to be handled, not just rejected.

Why This Matters

Many real bugs live at boundaries: the average of an empty list divides by zero, a 'pass at 50 or more' check written as more than 50 fails the student who scored exactly 50, a name field rejects O'Brien. Thinking about edges while designing, not after users complain, catches these before release, and it forces you to decide what the program should do in situations the normal examples never show.

Intuitive Explanation

A ride at a theme park has a sign: riders must be at least 120 cm tall. The ordinary cases are easy: a 150 cm teenager rides, a 90 cm toddler does not. The interesting rider is exactly 120 cm. The rule says 'at least', so they ride. If the code says height > 120 instead of height >= 120, every ordinary test passes and that one child is turned away. Edge cases are the inputs where the rule and the code are most likely to disagree.

Most edges come from a short list of questions about the input. What if there is nothing, such as an empty list or a blank form field? What if there is exactly one item? What are the smallest and largest allowed values, and the values just past them (119 cm and 121 cm)? What if two values are equal, like two players tied for top score? Each answer becomes a test with an expected result you decide before running the code.

An edge case is not the same as invalid input. A height of -40 cm cannot happen, so the program should refuse it with a message. An empty class list can happen, for a new class with no students yet, so an average function should return something sensible instead of crashing. The rules of the problem tell them apart: if the situation can really occur, handle it; if it cannot, reject it clearly.

Think of it like this. Testing edge cases is like testing a bridge with the heaviest truck allowed, an empty road and a gale, not just a few cars on a calm day. Ordinary traffic shows the bridge works; the extremes show whether it holds. The difference is that code can fail at one exact value, such as 120, while 119 and 121 behave perfectly.

Core idea. Boundary inputs are where hidden assumptions tend to break.


When to Use

Look for edge cases whenever you write tests, review a function that loops over a list or divides, or see a rule with 'at least', 'up to' or a maximum length, and whenever a bug shows up for only some users. Try the empty, single, smallest, largest, tied and just-past-the-limit inputs. If the task is to disprove a general claim with one example, that is a counterexample; if a value is simply not allowed, it is input validation rather than an edge case.

Tip. Ask: Is the question about inputs at the limits the program must handle, such as empty, zero, one item or exactly on a boundary?

How to Recognize It

Look past the typical input and find the values at the edges of what the program accepts.

  1. Does the input have an allowed range, such as ages 13 to 18, and is the test value right at one end?

    Yes means a boundary edge case: test 13 and 18 themselves, plus 12 and 19 just outside, which should be refused. A typical value like 15 is only an ordinary test.

  2. Could the input be empty, zero or a single item?

    Yes means test it, because loops and averages often break there; the average of an empty list divides by zero. A full, ordinary list only shows that the normal case works.

  3. Is the unusual value one the program is meant to accept?

    Yes means it is an edge case and needs a correct answer. A value that is never allowed, like an age of -5, calls for input validation that rejects it instead.

  4. Is the question about how to organize and run the checks, rather than which inputs to choose?

    Yes means it is about Unit Testing or Testing; edge cases are the inputs you pick for those tests.

Edge Cases vs Testing vs Unit Testing vs Input validation

Edge cases are used in testing and often sit right next to validation code, so the four names land in the same answer. Ask whether you are choosing which inputs to try, checking a program in general, checking one function in code, or refusing values that are not allowed.

Edge Cases

Meaning
Edge cases are allowed inputs at the limits of what a program handles, where ordinary-looking code often breaks.
Key test
Which inputs to try: empty, zero, one item, exactly on a boundary.
Example
For a rating box that takes 1 to 10, try 1, 10 and a list with a single rating.

Testing

Meaning
Testing runs a program with chosen inputs and checks that each output matches the expected one.
Key test
The checking activity, using normal and edge inputs alike.
Example
Typing five ratings into the app and checking that the average it shows is right.

Unit Testing

Meaning
Unit testing is code that checks one function at a time with fixed inputs and expected outputs.
Key test
Automated checks on one function, which can include edge cases.
Example
assert average([]) == 0 is a unit test that checks an edge case.

Input validation

Meaning
Input validation is code that refuses values the program should never accept, before they are used.
Key test
Rejects values outside the allowed range; edge cases sit inside it.
Example
Showing 'Enter a number from 1 to 10' when someone types 11 or abc.

Formula & Notation

x∈{∅,0,min,max}
Edge cases are inputs near boundaries of the valid input domain or at transitions where program behavior changes qualitatively.

Worked Examples

Example 1 — Choose test ages for a ticket rule

Easy

A cinema program charges nothing under age 3, the child price for ages 3 to 12 inclusive, and the adult price from 13 up. Choose edge-case ages to test and give the expected price for each.
Show the steps and the answer
  1. Mark where one price hands over to the next: between 2 and 3, and between 12 and 13.

    Mistakes collect where one rule ends and the next begins, so those are the values to test.

  2. Test each boundary and its neighbour: 2 free, 3 child, 12 child, 13 adult.

    A pair on each side of a line shows whether the code used < or <= correctly.

  3. Add the extremes: 0 free (a baby is a valid customer) and 110 adult.

    The smallest real age and a very large one show the ranges have no gaps at their ends.

  4. Add one impossible input: -1 should be rejected with an error message.

    It is not a real age, so refusing it is correct, which is different from handling a valid edge.

Answer: Test 0 (free), 2 (free), 3 (child), 12 (child), 13 (adult), 110 (adult) and -1 (error).

Takeaway: For a rule with ranges, test each boundary and the value just past it, plus the extremes and one impossible input.

Example 2 — Edge case or invalid input?

Standard

A class-average function is given three inputs by different teachers: (a) a list with one score, [88]; (b) an empty list, because a new class has taken no tests yet; (c) a list containing the word 'absent'. For each, decide whether it is a valid edge case or invalid input, and what the function should do.
Show the steps and the answer
  1. (a) One score is valid: a class can have taken just one test.

    The average of [88] is 88 / 1 = 88, so the code must not assume at least two values.

  2. (b) An empty list really can happen, so it is a valid edge case.

    A sum of 0 divided by a count of 0 is undefined, so the function needs a result chosen in advance, such as 'no scores yet', instead of crashing.

  3. (c) The word 'absent' is not a score, so it is invalid input.

    Averaging it means nothing; the function should refuse it with a clear message, or the caller should remove it before calling.

Answer: (a) Valid edge case: return 88. (b) Valid edge case: return 'no scores yet' instead of dividing by 0. (c) Invalid input: refuse it with an error message.

Takeaway: If a situation can really occur, give it a defined result; if it cannot be a real value, reject it clearly.

Example 3 — Find the mistake: assuming at least one item

Application

Eli writes a function to find the longest word: 'SET longest TO words[0]. FOR each w IN words: IF length of w > length of longest THEN SET longest TO w. RETURN longest.' He tests it on ['cat', 'giraffe', 'dog'], gets 'giraffe', and says it is finished. Find the mistake and correct it.
Show the steps and the answer
  1. Run the empty list []: words[0] does not exist, so the first line fails.

    Eli's only test had three items, and the code silently assumes there is always at least one word.

  2. Run a tie, ['bear', 'lion']: both have 4 letters, and the > comparison keeps 'bear'.

    First-longest-wins is a reasonable rule, but it should be a decision Eli knows about, not an accident.

  3. Fix it: IF words is empty THEN RETURN 'no words', placed before the first line. Then test [], ['ox'], ['bear', 'lion'] and ['cat', 'giraffe', 'dog'].

    These cover nothing, one item, a tie and the normal case; the expected results are 'no words', 'ox', 'bear' and 'giraffe'.

Answer: Check for an empty list before reading words[0] and return 'no words'. Then [] gives 'no words', ['ox'] gives 'ox', ['bear', 'lion'] gives 'bear', and ['cat', 'giraffe', 'dog'] gives 'giraffe'.

Takeaway: Passing one normal test is not enough; try empty, single and tied inputs before calling a function finished.

Where You'll See It

  • A calculator app dividing by zero

    Normal division works fine, but dividing by zero is an edge case that can crash or freeze the app if the programmer never tested it.

  • A name field on a sign-up form

    Most names are simple, but edge cases like an empty name, a name with an apostrophe (O'Brien), or a 200-character name reveal whether the form is truly robust.

  • A grade-averaging program with no grades entered

    Averaging a list of test scores works until the list is empty; that boundary case (zero items) can cause a divide-by-zero or crash if not handled.

Common Mistakes

  1. The slip: If the program works on the normal examples, it works.

    Why it's tempting: Typical inputs are what you naturally think of and test first, and they pass.

    Instead: Bugs hide at the boundaries: empty input, zero, the minimum and maximum values, and unexpected formats. Robustness is shown by testing the edges, not the middle.

  2. The slip: Every weird-looking input is invalid and should be rejected.

    Why it's tempting: Unusual inputs look like errors, so rejecting them feels safe.

    Instead: Some boundary inputs (like 0, an empty list, or the maximum allowed value) are valid and must be handled correctly; you must decide case by case, not reject all unusual inputs.

  3. The slip: You can assume a list or array always has at least one element.

    Why it's tempting: In practice there's usually data, so the empty case rarely comes up while you're building.

    Instead: Code should check for the empty/zero case explicitly; assuming 'at least one item exists' is a hidden assumption that breaks at the boundary.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. average(nums) returns sum ÷ count. Which input breaks it, and what should the function do with it?

    Hint: Try the smallest possible list.

  2. largest(nums) returns the biggest number in a list. Give four edge-case inputs to test, with the expected output for each.

    Hint: Think empty, one item, ties, and all negative numbers.

  3. A ticket app charges the child price for ages 5 to 12 inclusive. Which ages should you test at the boundaries, and what is expected for each?

    Hint: Test each end of the range and one step outside it.

  4. last_char(s) returns s[len(s) - 1]. What does it give for 'cat', for 'a', and for the empty string ''?

    Hint: Work out len(s) - 1 for each input.

  5. Claim: 'Every number squared is bigger than itself.' You try x=12. Is that edge-case testing, or a counterexample?

    Hint: Are you checking a working rule at its limits, or disproving a claim about every number?

  6. A pizza-order form rejects an order with 0 toppings. The student who built it says: 'Zero is weird, so it must be invalid.' Correct them.

    Hint: Is a plain pizza a real order?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

What is an off-by-one error?

An off-by-one error is a loop or comparison that is wrong by exactly one, such as using < where <= was needed, so the last item or the boundary value is skipped. A fence 10 meters long with a post every meter needs 11 posts, not 10, and code makes the same slip. Edge-case tests catch these, because a boundary input is exactly where the extra or missing step shows up.

How do I find edge cases I have not thought of?

Go through a short list for every input: empty, zero, one, negative, the smallest and largest allowed values, and just past each end. For text, add blank, very long, and characters such as apostrophes or emoji. For dates, add 29 February and the last day of the year. Then ask a classmate to try to break your program; people who did not write the code find edge cases much faster.

Why did the Y2K problem happen?

Many programs written from the 1960s to the 1990s stored years as two digits to save memory, so 1999 was 99. The year 2000 became 00, which those programs could read as 1900, an edge case nobody had tested for decades. Companies spent billions checking and fixing code before 1 January 2000, and because of that work few serious failures happened. It shows that an edge case can sit years in the future when code is written.

Learning Path

← Before

Testing
Edge Cases

You are here

Next →

Unit Testing
You should know Testing: running a program with chosen inputs and comparing the output with what you expect. Edge cases add a method for choosing the inputs most likely to expose bugs, at the boundaries of the rules and the data. Next, Unit Testing turns each edge case into an automatic check on a single function that reruns every time the code changes.

See Also