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
1 answer
Force classes to implement one of a set of methods
Let's say I need a group of classes to implement all of methods X, Y and Z to work properly. In that case, I can let them implement a common interface to force that.
Now, my case is a bit different - my classes need to implement at least one of X, Y…

Martin Grey
- 744
- 2
- 12
- 26
0
votes
2 answers
How to index arrays using pointers safely
Edit: If you fundamentally disagree with the Fedora guide here, please explain why this approach would be worse in an objective way than classic loops. As far as I know even the CERT standard doesn't make any statement on using index variables over…

AdHominem
- 1,204
- 3
- 13
- 32
0
votes
1 answer
How can I isolate parts in a web page so that one part would not affect other parts
I am managing a project in which development is done by multiple developers working on different parts of a web page. We are facing integration issues when combining parts of a web page due to side effects.
If a web part has an error, is poorly…

Sambhav
- 1
- 3
0
votes
2 answers
A question about checking for the existence of a file versus the directory being empty and reliability
I know that pretty much every programming language has a method to check the existence of a file or directory.
However, in my case, a file is made which stores program settings. If it does not exist (ie !File.Exists or Directory.Count == 0 where…

GurdeepS
- 65,107
- 109
- 251
- 387
0
votes
2 answers
How defensive should one be when returning an object reference that is merely a private member "obfuscated" somehow?
Consider this C# program:
using System;
using System.Collections.Generic;
using System.Linq;
namespace SandboxApplication
{
public class IntsOwner
{
private List _ints;
public IntsOwner (IEnumerable ints)
…

Hammerite
- 21,755
- 6
- 70
- 91
0
votes
6 answers
How else can this code be optimized for defensive programming?
For my data structures project, the goal is to read in a provided file containing over 10000 songs with artist, title and lyrics clearly marked, and each song is separated by a line with a single double quote. I've written this code to parse the…

Jason
- 11,263
- 21
- 87
- 181
0
votes
2 answers
Checking preconditions on parameters in public methods
I'm going to ask your Point of view about a design matter.
The question is basically the following: a public method of an object should always check preconditions in its input parameters or is it better to love responsibility to the caller and…

Enrico Massone
- 6,464
- 1
- 28
- 56
0
votes
3 answers
Can this class be made more immutable?
package main;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public final class Tutor {
private final String name;
private final Set tutees;
public Tutor(String name,…

Anthony
- 13
- 5
0
votes
3 answers
How can I check arguments in a method from an interface without breaking the DRY principle
I have the following code:
public interface Person {
/***
*@Throws Exception x must be greater than 0 ****/
setAge(int x);
}
public class Man implements Person {
setAge(int x) {
if(x <= 0) thrown new Exception("x <= ");
…

jgleoj23
- 262
- 2
- 9
0
votes
3 answers
Test whether or not log-in system is protected against sql injection
So for a school project I have to make a site with a log-in system. It has a username and password field, and a submit button. It compares the username and password with those in a MySQL database. If the combination is in the database, the user may…

JJJ
- 1,009
- 6
- 19
- 31
0
votes
1 answer
Could preg_match be the only line of defense?
I was wondering that, if preg_match() could be used as the only line of defense on PHP.
Testing preg_match(), at least for a simple input form field, it accepts only what's acceptable by regex and returns false for everything else:
For…

William
- 1,010
- 3
- 21
- 39
0
votes
4 answers
Robust Code framework?
I hate writing code that makes my software more solid. This is something the framework should have done! So, is anybody aware of a code "enhancing" utility that solidifies the code?
If I had to create something like this myself, it would work as…

Avi
- 15,696
- 9
- 39
- 54
0
votes
0 answers
Where to test setters of entities?
I am writing unit tests for a Spring web application that uses Hibernate for data persistence. I haven't developed the application. I wonder whether it is a good idea to write JUnit tests testing setters of entities to ensure that defensive…

Limbo Exile
- 1,321
- 2
- 21
- 41
0
votes
1 answer
Handling undefined fields when creating a custom javascript object
I've always created javascript objects in the following way:
//object code
function someObject() {
this.field1;
this.fiend2;
}
function makeSomeObject(data) {
var result = new someObject();
result.field1 = data.field1DataName;
…

Ben Pearce
- 6,884
- 18
- 70
- 127
0
votes
3 answers
javascript question mark ?: logical syntax, defensive programming
I am trying to re-write the statement below using the javascript ?: syntax.
if(type of someVariable !="undefined"){
someFunction(someVariable);
}else{}
This is my current attempt and it's causing a syntax error
typeof someVariable !=…

Ben Pearce
- 6,884
- 18
- 70
- 127