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
4
votes
7 answers
C Syntax Question
Is it considered bad coding to put a break in the default part of a switch statement? A book I was reading said that it was optional but the teacher counted off for using it.

shinjuo
- 20,498
- 23
- 73
- 104
4
votes
2 answers
Will the macro `assert` be removed in C++20?
According to cppreference, assert will be used as a C++ attribute.
However, there already exist tons of projects heavily dependent on the macro assert, is there any bad effect?

xmllmx
- 39,765
- 26
- 162
- 323
4
votes
2 answers
How to avoid defensive if conditions in python?
I am writing a code which tries to dig deep into the input object and find out a value lying inside that object. Here is a sample code:
def GetThatValue(inObj):
if inObj:
level1 = inObj.GetBelowObject()
if level1:
level2…

Sadanand Upase
- 132
- 1
- 15
4
votes
2 answers
Objective-C defensive copying in accessor methods
Coming from a Java background, I'm having trouble figuring out ways to program defensively in Objective-C.
Assuming SomeClass is mutable and provides a copy method, this is a typical piece of code I'd write in Java:
public MyClass
{
private…

Dmitry
- 161
- 1
- 1
- 10
3
votes
3 answers
GSON vulnerabilities or exploits on deserialization to avoid
I am planning to use gson's fromJson() method to parse a string coming from the browser. Are there any potential vulnerabilities associated with doing that? The data type I am converting to is relatively simple, a List and a boolean. But since gson…

Elijah
- 1,252
- 3
- 21
- 32
3
votes
1 answer
Kotlin: Specify input-constraints in interface
Lets say I have the following interface:
interface MathThing {
fun mathFunction(x : Int)
}
Let's say the constraint I want to put onto this function is that x cannot be negative.
How can I make sure that every time this (or any other…

Moritz Groß
- 1,352
- 12
- 30
3
votes
1 answer
How to report error indices in R?
Update: Question here is closed, now discussed on RStudio Community Platform.
I'm trying to program defensively in my package development, using a lot input validation.
In particular, I'm relying on a lot of the ready-made assertions in checkmate,…

maxheld
- 3,963
- 2
- 32
- 51
3
votes
1 answer
How to defensively create Rx Observables and avoid race conditions?
I was hunting a weird edge case where a list of files in a directory didn't show results for 0...2 files but worked fine for 3...n files.
It turned out that the original observable sequence worked just fine. But I used a PublishSubject in one…

ctietze
- 2,805
- 25
- 46
3
votes
3 answers
Obtaining a reference to an instance whose constructor had thrown an exception
Consider the following issue
When designing a framework, an interface exposing some event is presented
interface I
{
event MyEventHandler MyEvent
}
This interface will eventually be implemented by many different 3rd party vendors, and may be…

Eyal Perry
- 2,255
- 18
- 26
3
votes
5 answers
Defensive database programming- robust code with T-SQL?
In the application development there is a concept of defensive programming. How to implement defensive programming techniques and writing robust code using Transact-SQL?

juur
- 5,633
- 10
- 32
- 36
3
votes
1 answer
Why is the MVC paradigm best suited for web applications?
I'm fairly certain my professor will ask me why I chose to use MVC for my web application.
Truth be told, I'm new to MVC. I read about it, I'm building a blog application using it, I think it's very logical to approach a problem this way.
But why?…
delete
3
votes
3 answers
Defensive programming against malicious attacks
The company that I work for is redeveloping an in-house product for external use.
The product will initially be developed in C# using WPF, then ported to Silverlight.
One of the focus points is coding against malicious attacks e.g. SQL injection…

user173847
- 138
- 6
3
votes
4 answers
Does User.Identity.Name ever throw?
I notice User.Identity.Name appears to return the empty string when there is no user authenticated, rather than throwing a null reference exception.
It is valid to assume User.Identity.Name will never throw? Or should I be checking for null…

fearofawhackplanet
- 52,166
- 53
- 160
- 253
3
votes
5 answers
Defensive anti-multithreading class implementation
I have an object that is not safe for multithreaded applications (in several ways), and I want to provide an internal check to make sure that critical methods are not accessed concurrently.
Question
What techniques should I employ to detect and…

makerofthings7
- 60,103
- 53
- 215
- 448
2
votes
2 answers
Is this defensive programming?
I've always thought defensive programming was evil (and I still do), because typically defensive programming in my experience has always involved some sort of unreasonable sacrifices based on unpredictable outcomes. For example, I've seen a lot of…

void.pointer
- 24,859
- 31
- 132
- 243