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
1 answer
IOrderedEnumerable and defensive programming
I'm fond of defensive programming.
I hate exception throwing, but this is not the subject of my question.
I adapted an extension to linQ to be able to perform an order by with a column name
public static IEnumerable OrderBy(this…

Mose
- 1,781
- 3
- 16
- 35
2
votes
4 answers
Carried throws declarations for RuntimeExceptions
Suppose I have a method or a constructor that uses another method or constructor internally that declares a RuntimeException can be thrown.
// Example:
public MyClass(Object arg) {
setVar(arg);
// Not responsible for dealing with the…

Samuel
- 18,286
- 18
- 52
- 88
2
votes
2 answers
defensive programming on a menu. what type of input should I have?
I tried to have defensive programming having input as an integer but the program would go into infinite loop if I entered a character.
I then switched to having input as a character, but it does the loop twice. Once for the wrong character and once…

n17n
- 81
- 5
1
vote
5 answers
Clojure : Maps of Lists of Maps of ... How to wrangle anyonymous data structures
Hi guys : In java we've all had the experience of using our ide to "traverse" down the depths of a complex data type :
dog.getCollar().getCollarTag().getName();
However, in Clojure, this becomes non trivial, due to the lack of static typing. How…

jayunit100
- 17,388
- 22
- 92
- 167
1
vote
1 answer
Main security concerns in allowing users embed video
I wanna allow users to embed videos freely in the application in developing, but do not want to expose then and the application to malicious uses.
With that in mind, what are the main security concerns (XSS, etc) in allowing users to embed videos…

lsdr
- 1,245
- 1
- 15
- 28
1
vote
1 answer
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 statement under the prompt-time function and this…

Vermillion Orange
- 91
- 6
1
vote
2 answers
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 be corrupted, some may not be to specification. In…

Boris Reif
- 123
- 1
- 10
1
vote
3 answers
C++ defensive programming: reading from a buffer with type safety
Let's say I have a class that I don't own: DataBuffer. It provides various get member functions:
get(uint8_t *value);
get(uint16_t *value);
...
When reading from a structure contained in this buffer, I know the order and size of fields, and I…

Cat Zimmermann
- 1,422
- 2
- 21
- 38
1
vote
2 answers
Control user input in C when there is chars and ints mixed
So I'm trying making a simple menu to a program that only accepts the options 1,2 and 3 and I want to control the user input to prevent errors like when the user inputs char instead of int.
Right now it controls some cases like if the option is less…

Rune
- 13
- 5
1
vote
1 answer
Parasoft rulewizard
I'm trying to create a static rule to check the token passing for defensive programming in a SIL4 application.
The rule is the following: "Each functions shall have a const uint_32 as last parameter"
ie:
uint_32 foo(uint_32 a, uint_32 b, const…

Steak_over_floor
- 19
- 2
1
vote
1 answer
How to deal with optional data in a typed domain model?
When developing applications / domain models in general, I am wondering what would be the best way to deal with incomplete or optional data.
Typed languages such as TypeScript and C# give us the power of typing our models. Sometimes this is…

Wouter van Koppen
- 915
- 7
- 26
1
vote
1 answer
No Exists method so I want to use AsQueryable for defensive programming
I am writing some code which queries the Visual Studio object model.
I see that there is no Exists method on the Projects collection object but I do like defensive programming and not relying on try catch blocks. So I see there is AsQueryable() on…

S Meaden
- 8,050
- 3
- 34
- 65
1
vote
1 answer
Why is defensive programming needed if source code is hidden?
Apologies if the question isn't too good, this is my first post.
I know that in Java defensive programming is a good thing to do, as it stops values of variables being changed if they are not supposed to be. However, if a program is compiled is this…

mikesimbo123
- 33
- 6
1
vote
3 answers
Checking for "nested" null attributes in Java
I've got a simple method that checks there are no null in attributes:
public boolean isValid(){
return session.getX() != null && session.getX().getY() != null &&
session.getX().getY().getZ() != null;
}
This works, but, Is there any way to…

stack man
- 2,303
- 9
- 34
- 54
1
vote
1 answer
Marking methods which do not read/write instance state
I have seen that sometimes methods in a class that do not depend on the instance state are marked as static. I find that it makes it quite clear in that it advertises that 'this method is independent of instance state'. It is easy to skip that…

Teddy
- 4,009
- 2
- 33
- 55