Skip to main content

Questions tagged [defensive-programming]

Defensive programming is a form of defensive design intended to ensure the continuing function of a piece of software in spite of unforeseeable usage of said software. Defensive programming techniques are used especially when a piece of software could be misused mischievously or inadvertently to catastrophic effect.

defensive-programming
0 votes
3 answers
838 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 ...
Youzohka's user avatar
1 vote
1 answer
82 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 ...
Vermillion Orange's user avatar
1 vote
2 answers
558 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 ...
Boris Reif's user avatar
-2 votes
1 answer
526 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: ...
CPlus's user avatar
  • 4,378
2 votes
2 answers
247 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" ...
Erik Andersson's user avatar
-3 votes
2 answers
809 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 ...
Poison's user avatar
  • 389
4 votes
0 answers
690 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 ...
Astemir Almov's user avatar
2 votes
1 answer
101 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 ...
Vega180's user avatar
  • 768
0 votes
0 answers
42 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 ...
Siddharth Shyniben's user avatar
11 votes
1 answer
506 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'...
Raymond Hettinger's user avatar
0 votes
1 answer
237 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 ...
kcantor's user avatar
  • 25
0 votes
1 answer
201 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, ...
Tim's user avatar
  • 97.8k
0 votes
2 answers
536 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 ...
LycheeSojuYYDS's user avatar
13 votes
2 answers
19k 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 ...
SuperJMN's user avatar
  • 13.8k
0 votes
2 answers
735 views

Kotlin: Condition that double-value is a "normal" number

How can I test if a value of type Double in Kotlin is not Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN or some other special value? I'd like to have something like require(Double....
Moritz Groß's user avatar
  • 1,422

15 30 50 per page
1
2 3 4 5
10