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.
Questions tagged [defensive-programming]
148 questions
0
votes
3 answers
What happens if I post to a site a million times repeatedly?
I was just trying to post something to a website via my localhost to retrieve some data, and suddenly, this idea came to my mind: What happens if I create a post, put it into a for loop that runs over 1 million times, and send requests to a specific…

Shaokan
- 7,438
- 15
- 56
- 80
0
votes
1 answer
Improving setValueForKeyPath robustness
I have created an extension to NSObject to allow for object properties to be set from data contained in a PLIST or dictionary. I did use setValuesForKeysWithDictionary but this only works for keys not keyPaths. My extension does the same thing,…

Magic Bullet Dave
- 9,006
- 10
- 51
- 81
0
votes
1 answer
Create a matrix MxN with integers inputs with a condition in the digits of the elements (python)
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 user should not use integers like…

kcantor
- 25
- 5
0
votes
1 answer
How can this code be changed to have no vulnerability to arithmetic overflow?
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, size_t ele_size) {
5 /*
6 * Allocate buffer for…

Tim
- 1
- 141
- 372
- 590
0
votes
3 answers
How to prevent outliers to be inserted in database?
I have a MS SQL DB contains set of tables each table represents a collection of variables calculated based on our formulas.
All the variables are numeric with predefined percision (we are using numeric data type with n.m as n number of digits for…

Ahmed
- 7,148
- 12
- 57
- 96
0
votes
2 answers
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 necessary. From my understanding, if the parse function…

LycheeSojuYYDS
- 45
- 1
- 9
0
votes
2 answers
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.isNormal(x))

Moritz Groß
- 1,352
- 12
- 30
0
votes
1 answer
Is there a way to enforce that a kotlin function can be run only in the context of another function?
I'm trying to reply this answer Kotlin: Specify input-constraints in interface and my idea was to create a function that implements some validation and allowing the function to be validated to be called only in the context of the validation…

Diego Marin Santos
- 1,923
- 2
- 15
- 29
0
votes
5 answers
How do I switch over an enum class?
Enum classes are supposed to be strong enums in the sense that they don't implicitly convert to and from int. For instance:
enum class EC { a, b };
However, when switching over such a "strong enum":
int sw(EC ec) {
switch (ec) {
case EC::a:…

bitmask
- 32,434
- 14
- 99
- 159
0
votes
1 answer
Expose private case class in Scala
I have a situation where a codepath is given some Path's which it uses to query a database. It so happens that sometimes some of these paths need to go through a translation service, to map them to updated Paths before the query. Essentially the…

User
- 161
- 1
- 8
0
votes
2 answers
When checking if an array is empty, should I use array.length <= 0 or array.length === 0?
I am used to check for empty arrays with array.length <= 0. Of course the array should never have a length smaller than 0. This is a habit I developed to make sure my program runs "just in case something weird happens". Is there any reason not to…

eddex
- 1,622
- 1
- 15
- 37
0
votes
2 answers
Initialization in a parameterless constructor should be avoided?
some of my colleagues demand to avoid parameterless constructor and it's initialization completly.
Because they tell me that a default initialization of a string property for example quickly hides the ability to find out the source of the error.…

Benjamin Martin
- 576
- 1
- 8
- 27
0
votes
0 answers
Is there / should there be a way to track the validity of a raw pointer taken from a smart pointer for debug/QA purposes?
As I understand C++ Core Guidelines by Bjarne Stroustrup & Herb Sutter, it is a good idea to use pointers this way:
unique_ptr for the good old clear one ownership
shared_ptr for truly shared ownership by design
weak_ptr when by design the pointer…

Doron Ben-Ari
- 443
- 3
- 12
0
votes
4 answers
Is it a good practice to add extra checks in disabled GUI options?
I'm referring to the following:
void setup_gui()
{
if (some_condition)
some_button.disable();
...
}
void some_button_click()
{
// Is this a good practice?
if (some_condition)
return;
...
}
Adding that check…

GameZelda
- 824
- 1
- 7
- 13
0
votes
2 answers
add a defensive copy of an object to a hashset
Currently I have this code:
public final class Tutor {
private String name;
private final Set tutees;
public Tutor(String name, Student[] students){
this.name = name;
tutees = new HashSet();
for (int i = 0;…

pxdr0
- 73
- 7