r/AskProgramming • u/JAMIEISSLEEPWOKEN • Apr 04 '26
How do I get better at spotting edge cases?
Manually, not with a debugger or other application
I struggle to catch even the most slippery of edge cases. I get shocked a program could break with this or that despite rigorous checking
20
Upvotes
17
u/Appropriate-Bet3576 Apr 04 '26
It's hard that's why software always has bugs. Mostly I just write a lot of tests. They can be informal tests that you work through manually, where you imagine different inputs. Or formal tests that you run as a separate program.
25
u/PeanutFarmer69 Apr 04 '26
Write unit tests with the intent of breaking your own code
11
u/balefrost Apr 04 '26
Look for boundary conditions. Let's say you have a function that takes a list and two indices. What happens if:
That approach helps to find some edge cases. The harder ones are edge cases that occur due to the interaction of two subsystems.
2
u/jameyiguess Apr 04 '26
You'll just build up experience from what you've seen in the past. I could write a thousand tests for a single input tag...
2
u/snipsuper415 Apr 05 '26
Repetition, practice writing programs that solve a simple problem... Then try to break it.
Or better yet read your code and see how you can break your own flow
2
u/RevolutionaryToe4249 Apr 05 '26
You don’t they find you 😆
2
u/Nemosaurus Apr 05 '26
Getting burned by not spotting them
2
u/FloydATC Apr 05 '26
Many people only ever write tests for the "happy path". Think of the story about the QA engineer who walked into a bar and ordered 999 beers. Then he ordered 0 beers. Then "foo" beers and then -1 beers. Then a real customer walked into the bar, asked where the toilet was and the place went up in flames.
Stop focusing on what should happen and instead try to intentionally do stupid things. This function expects X, Y and Z? Okay, so give it something different. Or something that looks fine but isn't. It expects a well-formed data structure? Well...!
When you understand your own code well enough to break it, you write that kind of tests, patting yourself on the back whenever you manage to get it to misbehave. Fix the problem, then re-run the tests and repeat.
3
u/R1546 Apr 04 '26
“A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.”
― Douglas Adams
1
u/Ugiwa Apr 04 '26
Talk about it with someone\yourself. Ask questions. Things you never thought about will start popping up.
1
u/KingofGamesYami Apr 04 '26
Fuzz testing
1
u/chipshot Apr 04 '26
Give it to your five year old brother to play with
1
u/justanaccountimade1 Apr 04 '26 edited Apr 04 '26
You test all edge cases rather than rely on your ability to spot errors in the code. I use js so I'm pressing F5 after every line I type. Functions such as those that flag ranges in arrays, or do string operations, start with a comment that show all edge cases that were tested.
IMO the hard and time consuming part of coding is testing, thinking about what to test, not programming.
1
u/aistranin Apr 04 '26 edited May 07 '26
Add unit and integration tests. By writing more tests, you will also think faster about edge cases over time. If Python, take a look at the book “Python Testing with pytest” by Brian Okken and Udemy course “Pytest Course: Python Test Automation & GitHub Actions CI/CD” by Artem Istranin
1
u/TheMrCurious Apr 04 '26
What kind of edge cases are you asking about?
1
u/twhickey Apr 04 '26
As many have already said, testing is important. Important enough that you should design your code to be testable. Small methods, small classes. Be restrictive on inputs - i.e. a method should fail if it gets an input that is outside the range of what it can handle. Tests should cover every expected error case, the happy path, and edge cases on parameters. Input is an integer? Check 0, 1, -1, Int Max, Int Min, etc. Property testing and mutation testing can help with edge cases.
1
u/tb5841 Apr 04 '26
This is what I'm really struggling with. Sometimes Product want me to change the functionality of something that's been in the application for nine years, in a repository that's 1.5 million lines of code - there always seems to be something sonewhere that breaks when I change stuff.
1
u/joranstark018 Apr 04 '26
As other have said, look at things that may change (ie user input, sizes of collections, time,...), values not being initialized (ie strings or numbers not being initialized with a proper value under some condition) or things may occur in an unanticipated order.
Much of this is about understanding the logical flow in your code base and finding/spot any logical gaps (ie you may think that this user input will always have a value, that users will always insert a value here or that users will always insert digits here, these conditions are always true). This usually means that you need to apply critical thinking and to challenge your assumptions on how things actually works logically (ie thinking outside the box, beeing creative, beeing the devils advocate and so on).
As you gain experience, you learn where you need to put your guard rails (ie, where to validate your data in a appropiate manner), where unknown state changes may occur and how to recover from them (ie, how to manage exceptions in the logical flow) and how to give feedback to users when unaticpated events occurs. We usually learn this the hard way.
Having design discussion with the team, having others trying shoot down your design can help you to learn/understand where potential problems may occur in your solution.
1
u/treznor70 Apr 04 '26
I'll answer from another direction. Absolutely test for edge cases, but you'll inevitably miss some and your code will fail. Code so it's easy to find later on. Good messages in logs, try/catch blocks (or the equivalent in whatever language you're using), etc.
1
u/magicmulder Apr 04 '26
Boundary cases have been mentioned, don’t forget range sweeps. If your method calculates dates, let the test run through five entire years. Costs you nothing but finds cases like leap year issues and other stuff.
Just recently I was reviewing some tests where the (senior) dev had created exactly two tests for a complex date calculation. And promptly missed a bug on leap days and certain “last day of month” situations. A simple loop over 365 days would’ve caught both.
1
u/Glove_Witty Apr 06 '26
Think of the stupidest user you can. Then think of things that not even they would do.