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
7
votes
9 answers
Should I leave an unreachable break in a case where I throw an exception?
Is it silly of me to leave unreachable break statements in a case that just throws an Exception anyway? The defensive part of me wants to leave it there in the event that the logic changes. Another part of me doesn't want other developers seeing…

Tim Lehner
- 14,813
- 4
- 59
- 76
7
votes
8 answers
How to avoid key-loggers when authenticating access
As per the title really, just what can be done to defeat key/keystroke logging when authenticating access?
I have just posted a related question (how-to-store-and-verify-digits-chosen-at-random-from-a-pin-password) asking for advice for choosing…

andora
- 1,326
- 1
- 13
- 23
7
votes
3 answers
Why are fail fast style programs shorter than defensive style programs?
I have read about how the fail-fast style of programming in languages like Erlang end up with much shorter programs than the defensive style found in most other languages. Is this correct for all types of programs and what is the reasoning for this?

yazz.com
- 57,320
- 66
- 234
- 385
7
votes
14 answers
How defensive should you be?
Possible Duplicate:
Defensive programming
We had a great discussion this morning about the subject of defensive programming. We had a code review where a pointer was passed in and was not checked if it was valid.
Some people felt that only a…

TERACytE
- 7,553
- 13
- 75
- 111
6
votes
6 answers
Does defensive programming violate the DRY principle?
Disclaimer: I am a layperson currently learning to program. Never been part of a project, nor written anything longer than ~500 lines.
My question is: does defensive programming violate the Don't Repeat Yourself principle? Assuming my definition of…

jkeys
- 3,803
- 11
- 39
- 63
6
votes
2 answers
How well are Cocoa UI and general framework elements protected against malicious attacks?
So far I had little concern about overall security considerations, because I have been developing only promotional and uncritical iPhone apps.
Currently, however, I'm working on a Mac application which requires a few more thougts about the matter,…

Toastor
- 8,980
- 4
- 50
- 82
6
votes
7 answers
web application attacks and must have defence methods
What is your must have defence methods to common web attacks like XSS, Sql Injection, Denial of Service, etc. ?
Edit : I collected your responses under descriptions from Wikipedia. And I add some extra questions to have a complete reference.
Sql…

Canavar
- 47,715
- 17
- 91
- 122
5
votes
5 answers
C# anonymous backing fields with non-auto properties
I want to make a private member variable that is private even to the class that owns it, and can ONLY be accessed by its getters and setters.
I know you can do this with auto-properties like
private int MyInt{ get; set;}
But I want to be able to…

Bryan Hart
- 583
- 1
- 5
- 11
5
votes
3 answers
TDD vs Defensive Programming
Uncle Bob says:
"Defensive programming, in non-public APIs, is a smell, and a symptom, of teams that don't do TDD."
I am wondering how TDD can avoid an (internal) function to be used in an unintended way? I think TDD can´t avoid it. It merely shows…

offline
- 201
- 2
- 10
5
votes
2 answers
How do you manage external dependencies for your application?
There are many types of external dependencies. Interfacing with external applications, components or services (e.g. Outlook to send emails, TWAIN or WIA for scanning, ActiveX objects and Web services for various purposes, and so on).
What is your…

Ola Eldøy
- 5,720
- 7
- 49
- 82
5
votes
9 answers
How can I program defensively in Ruby?
Here's a perfect example of the problem: Classifier gem breaks Rails.
** Original question: **
One thing that concerns me as a security professional is that Ruby doesn't have a parallel of Java's package-privacy. That is, this isn't valid…

James A. Rosen
- 64,193
- 61
- 179
- 261
5
votes
2 answers
How to detect QObject::moveToThread() failure in Qt5?
The documentation on QObject::moveToThread() for Qt5.3 explains that the moveToThread() method can fail if the object has a parent. How would I detect this failure in my code?
I realize that simply making sure that my object does not have a parent…

Mr. Developerdude
- 9,118
- 10
- 57
- 95
5
votes
2 answers
Inefficiency of defensive copy in Java
I'm a longtime C/C++ programmer who's learning Java. I've read about the problem of breaking encapsulation by having an accessor method that returns a reference to a private field. The standard Java solution seems to be defensive copying - calling…

Dave Beal
- 61
- 3
5
votes
3 answers
XmlSerializer - How can I set a default when deserializing an enum?
I have a class that looks like this (heavily simplified):
public class Foo
{
public enum Value
{
ValueOne,
ValueTwo
}
[XmlAttribute]
public Value Bar { get; set; }
}
I'm receiving an XML file from an external…

NeilD
- 2,278
- 3
- 24
- 28
5
votes
2 answers
Adding *copies* of entries from Java Map propertyMap
I would like to add copies of a propertyMap to my propertyMap:
public void addProperties(Map propertyMap) {
for (Map.Entry propertyEntry : propertyMap.entrySet()) {
…

Robottinosino
- 10,384
- 17
- 59
- 97