DEV Community

Pulkit
Pulkit

Posted on

Do you test edge cases?

Often when we learn programming, we are never taught to think outside the box. Yes, we are given some set of requirements, and a vision of what to build. It is our job as Software Engineers to build that product.
So you folks might have heard of this, famous code to find factorial of a number, the recursive method.

int factorial(int n)
{
    int ans = 1;
    if(n == 0 || n == 1)
    {
        return 1;
    }
    ans = n * factorial(n-1);
    return ans;
}
Enter fullscreen mode Exit fullscreen mode

Question: What happens when we enter negative number?

A penny for your thoughts

Top comments (1)

Collapse
 
ccoveille profile image
Christophe Colombier

Coding is about handling the unexpected. So yes, if you have to code something preventing passing negative number. You know you expect to pass a positive number, you should raise an error.

But, of course, it's often uneasy to think about the strange edges cases others may face.

Think about it, by using int, you already avoided people to pass a decimal number.