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
2
votes
8 answers
Adding items to a List / defensive programming
Explicitly checking/handling that you don't hit the 2^31 - 1 (?) maximum number of entries when adding to a C# List is crazyness, true of false?
(Assuming this is an app where the average List size is less than a 100.)
CuriousBob
2
votes
2 answers
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" in my app views in Django, and he told me to remake the…

Erik Andersson
- 21
- 1
2
votes
0 answers
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 such libraries?
I'm speaking of cases like…

Astemir Almov
- 396
- 2
- 16
2
votes
1 answer
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 defensively, even when it is no third-party API -> I…

Vega180
- 779
- 2
- 5
- 22
2
votes
2 answers
How can I maintain correlation between structure definitions and their construction / destruction code?
When developing and maintaining code, I add a new member to a structure and sometimes forget to add the code to initialize or free it which may later result in a memory leak, an ineffective assertion, or run-time memory corruption.
I try to maintain…

user10530562
- 149
- 2
- 8
2
votes
4 answers
Should I throw checked or unchecked exception for invalid input?
I'm writing a method which expect some input. Now, I thought that it would be a good practice to write it in a defensive way (i.e. checking that the input is correct).
Basically, the input should be okay since there's only one place (reliable) that…

IsaacLevon
- 2,260
- 4
- 41
- 83
2
votes
2 answers
Replacing `goto` with a different programming construct
I m trying to do this little programm with defensive programming but its more than difficult for me to handle this avoiding the Loop-Goto as i know that as BAD programming. I had try with while and do...while loop but in one case i dont have…

Karajohann
- 39
- 6
2
votes
1 answer
What is the best way of sending defensive copy of data ?
I just have read effective java rule 39 (Defensive Copy). It is not told explicitly, two times of copy should occur every data transaction to follow this rule. Below is the example code I thought. It seems somewhat redundant. Am I understand it…

myoldgrandpa
- 871
- 7
- 21
2
votes
3 answers
process self defence like antivirus
I write program for defence system, antivirus anti malware etc. And i have a problem with defensing process from killing thru tast manager->Kill Process. I test some antiviruses and they dont let me to kill his process. I only can stop them in…

lebron2323
- 990
- 2
- 14
- 29
2
votes
0 answers
How to avoid accidental bit shift operation in for loop?
I had and error in my code where I accidentally typed i << foo.length() instead of i < foo.length() in my for loop condition, forcing the loop to immediately quit. I was wondering how I could avoid such error in the future.

Anže
- 181
- 8
2
votes
2 answers
How defensive to be when interacting with another internal microservice?
In this scenario there are two HTTP microservices:
The public service that provides the client with data
The internal microservice that authenticates calls to the public service
Service 1 makes a call to Service 2 to ask it to authenticate the…

edev
- 91
- 1
- 6
2
votes
3 answers
perl defensive programming (die, assert, croak)
What is the best (or recommended) approach to do defensive programming in perl?
For example if I have a sub which must be called with a (defined) SCALAR, an ARRAYREF and an optional HASHREF.
Three of the approaches I have seen:
sub test1 {
die…

knv17
- 101
- 6
2
votes
1 answer
Automated null checks
Vendors commonly give me sprawling, oversized proxy classes to pass information. Often times to get to the actual content of the message I have to descend into a property like this:
var priceOfEggs =…

micahhoover
- 2,101
- 8
- 33
- 53
2
votes
3 answers
How is this code prone to SQL injection?
I was reading PostgreSql documentation here, and came across the following code snippet:
EXECUTE 'SELECT count(*) FROM mytable WHERE inserted_by = $1 AND inserted <= $2'
INTO c
USING checked_user, checked_date;
The documentation states that…

A-K
- 16,804
- 8
- 54
- 74
2
votes
4 answers
Basic defensive programming
Possible Duplicate:
Favorite (Clever) Defensive Programming Best Practices
I am always advised by some programmers to pay concentration to easy debugging. What is defensive programming and to which extend should it be considered while practicing?…

ArK
- 20,698
- 67
- 109
- 136