128

I'm not a native English speaker. In my native language I'm aware of some terms used to refer to the condition checked to stop a recursion, and to the condition checked for extreme, unlikely or super-simple cases. In English, I've encountered the terms "edge case", "corner case", "boundary case" and "base case", but I can't quite figure out the differences and which is used to refer to what; I'd love to get some summary of the differences between them.

In particular, I would be very happy if someone could provide annotations for the lines in the following code sample:

    int transmogrify(int n) {
1.      assert(n <= 1000000);
2.      if (n < 0) return -1;
3.      if (n == 1000000) return PRE_CALC;
4.      if (n == 0) return n+1;            // For stopping the recursion
5.      if (n == 1251) return 3077;
        return transmogrify(n-1);
    }

I think it's:

  1. Sanity check
  2. Input check
  3. Boundary case? Edge case? Corner case?
  4. Base case? Boundary case?
  5. Corner case? Edge case?
2
  • 4
    I believe edge and boundary are generally the same. But they refer to testing, not to checking conditions in code.
    – Richard
    Dec 16, 2011 at 13:49
  • 1
    @Richard Testing not code? That's not my understanding - do you have a reference to support the fact that you believe these terms don't apply to code? Feb 20, 2017 at 18:10

2 Answers 2

127

I'm not a native English speaker either. But according to Wikipedia:

  • Edge case occurs at an extreme (maximum or minimum) operating parameter.
  • Corner case occurs outside of normal operating parameters, specifically when multiple environmental variables or conditions are simultaneously at extreme levels, even though each parameter is within the specified range for that parameter. (The "outside normal operating parameters" obviously means something like "outside typical combination of operating parameters", not strictly "outside allowed operating parameters". That is, you're still within the valid parameter space, but near its corner.)
  • Boundary case occurs when one of inputs is at or just beyond maximum or minimum limits.
  • Base case is where Recursion ends.

So, the nomenclature seems to be totally confused, even though corner case seems to mean something a bit different (a combination of values) than edge and boundary cases, which are definitely synonymes. It's probably safe to say that edge, corner, and boundary cases are the same thing in common speech. Someone could mean to say different thing by each of them, but there's hardly any common agreement.

Your 1) and 2) are what you wrote, 3) is a edge/boundary case, 4) is a base case, and 5) is a special case.

2
  • 1
    This is very useful answer to understand concepts behind each technical words. Oct 17, 2018 at 5:42
  • Very useful in interview
    – Yiling Liu
    Sep 8, 2020 at 6:39
33

Regardless of the differences between the words, what you'd use to describe a test depends on the semantics (meaning) of the test, not the exact code - In the example provided it's not obvious what each of the tests mean. That aside, here's how I understand them:

  • Sanity check = Does this even make sense? For example, if your application only outputs integers, sqrt(-1) and log(-1) are undefined.
  • Input check = Tests the user input, as opposed to some internal data structure or the output of a function. For example, in Bash [ $# -gt 0 ] checks that you got at least one input parameter, which could also be a sanity check for a command like find or mail.
  • Edge / Boundary check = The maximum or minimum input which is expected to produce correct output. For example, a function which just adds one to a number will have an operating range from <MIN_INT> to <MAX_INT> - 1, since input smaller than <MIN_INT> can't be provided by the user and output larger than <MAX_INT> won't be useful.
  • Corner case check = A more complex boundary check (a corner is a two-dimensional boundary), for example combining <MIN_INT> and <MAX_INT> in a calculation.
  • Special case check = Non-obvious, non-boundary special values, for example log(1 + the smallest floating point number).
1
  • An unsophisticated nit: logₑ-1 is iπ. In python, import cmath; assert(cmath.log(-1) == 3.141592653589793j)
    – Bob Stein
    Feb 20, 2017 at 14:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.