147 questions
-3 votes
2 answers
86 views
Hypothesis: test defensive programming
I'm writing tests with Hypothesis to test functions with restricted range. Consider the following toy example: import math from hypothesis import assume, example, given def inc(value: float) -> ...
0 votes
3 answers
1k views
using try/except & while loop for calculator in python
I am trying to create a calculator and use defensive programming to avoid unexpected events and user inputs. I included a try/except block for user input & for zero division. When I input the ...
1 vote
1 answer
106 views
JS Defensive Error Handling try catch while loop error
I am writing code to learn defensive error handling in javascript. The program calculates distance, time or speed depending on the user's choice. This bit works fine. I then tried to add a try catch ...
1 vote
2 answers
876 views
How to ignore corrupted files?
How to loop through a directory in Python and open wave files that are good whilst ignoring bad (corrupted) ones? I want to open various wave files from a directory. However, some of these files may ...
-2 votes
1 answer
732 views
Can [NSApplication sharedApplication] ever return nil?
In a MacOS app in Objective-C is it possible for [NSApplication sharedApplication] to return nil or is it a safe assumption that this will never return nil? The Documentation describes the behavior: ...
2 votes
2 answers
326 views
Defensive programming for delete function in views Django
I am fairly new to Django, and I got some feedback for my project (recipe app) that I am currently working on from my mentor about defensive programming. I have created a delete "function" ...
-3 votes
2 answers
1k views
Why doesn't the rangeCheck method in the java.util.ArrayList class check for negative index? [closed]
/** * Checks if the given index is in range. If not, throws an appropriate * runtime exception. This method does *not* check if the index is * negative: It is always used immediately prior to an ...
5 votes
0 answers
880 views
Nullable reference types and null-oblivious libraries
Recently we started to use C# nullable reference types in our projects. Of course in our project, we use nuget libraries that don't support nullable reference types. What is the best practice of using ...
2 votes
1 answer
117 views
How to request data from Firestore defensively with Flutter
Since Firestore is a NoSQL database and has no strict type rules and defined document structures, I think about handling corrupt data in my Flutter app. In case you wonder why I want to request ...
0 votes
0 answers
57 views
Advice on refactoring lots of consecutive if checks
I have some code like so: export async function handleRefresh() { if (!existsSync('postr.toml')) fail('not a postr directory'); const posts = expandGlob('posts/*'); for await (const post ...
11 votes
1 answer
562 views
Avoiding accidental capture in structural pattern matching
This example is being discussed as likely "gotcha" when using pattern matching: NOT_FOUND = 400 retcode = 200 match retcode: case NOT_FOUND: print('not found') print(f'...
0 votes
1 answer
269 views
Create a matrix MxN with integers inputs with a condition in the digits of the elements (python) [closed]
I would like to create a matrix MxN. This matrix gets the inputs from the user. I would like to use defensive programming such that the elements of the matrix have not the digit 2. For example the ...
0 votes
1 answer
210 views
How can this code be changed to have no vulnerability to arithmetic overflow? [closed]
Computer Systems: a Programmer's Perspective says: 1 /* Illustration of code vulnerability similar to that found in 2 * Sun’s XDR library. 3 */ 4 void* copy_elements(void *ele_src[], int ele_cnt, ...
0 votes
2 answers
871 views
Is this additional check for parsing a string to LocalDate object necessary?
I was writing some tests for some legacy code that validates a user's date of birth. I encounter the following method in the class. My doubt is that whether the if statement in the try block is ...
14 votes
2 answers
23k views
How to use the Either type in C#?
Zoran Horvat proposed the usage of the Either type to avoid null checks and to not forget to handle problems during the execution of an operation. Either is common in functional programming. To ...