Questions tagged [defensive-programming]
Defensive programming is a style of programming designed to minimize the possibility of bugs introduced by code changes or unforeseen usage of the software.
26 questions
2
votes
1
answer
347
views
Is it possible to restructure code to avoid this copy-paste bug? [closed]
I came across quite a subtle bug the other day, where there are two sections of similar code that were supposed to use different variables, but copy-pasting had lead them to use the same variable. I'...
-2
votes
1
answer
81
views
Automated Encryption Keys
A question that I have been wondering for quite some time now, is how do two devices send encrypted data over the internet know the encryption keys to encrypt and decrypt the data, or more importantly ...
1
vote
3
answers
370
views
Is `print statements` a defensive programming technique?
We had an exam and the question paper asked which of the following is not a defensive programming technique. I've answered it as print statement, that a print statement is not a defensive programming ...
3
votes
4
answers
338
views
Should a transformation function take a nullable and return a nullable or should the caller handle nullability
Often I need to transform a type to another, such as a networking model to a data model, or a data model to a binary representation. Should these transformation functions take an Optional/nullable ...
1
vote
5
answers
1k
views
Limits of Defensive Programming acknowledging that Exception Handling should be avoided
I've read Defensive Programming vs Exception Handling? and if/else statements or exceptions, but none contain something relevant to what I'm searching for.
Taking into account that exception handling ...
2
votes
1
answer
774
views
Is using the copy constructor in an object's construction is bad? [duplicate]
I was reading this page about using the new keyword in the constructor, and I was wondering if it applies to copy constructors for collections.
Suppose I have a Book class and a collection to store a ...
0
votes
1
answer
65
views
Dealing with server errors/database data when processing client requests
Suppose my server PHP end point follows something along the lines of the following:
Client sends request to server.
Server updates a record
Server might do some PHP processing...
Server updates record ...
3
votes
3
answers
207
views
Should I use a physical units library for modelling domain properties if I don't need to perform computations on them?
I'm working on the data model of a service describing houses and flats. This involves storing quantitative physical properties of certain features of the premises, for example:
Speed of the Internet ...
1
vote
1
answer
546
views
Is a General Exception In Addition To Expected Exceptions Defensive or Unecessary?
I have read this and this and this: if my question misses the point in those answers, please point it out and we'll get this one deleted. These questions indicate that this may actually be a bad thing ...
2
votes
2
answers
1k
views
Stack implementation by Design by Contract vs Defensive Programming
I am trying to write Stack code using the two techniques i.e Design by Contract vs Defensive Programming but I am not sure if I am doing right or not.I am not throwing any kind of exception or error ...
5
votes
3
answers
4k
views
What is the difference between debugging and antibugging?
The terms debugging and antibugging seem to be widely used for referring to tools, measures and design patterns to get rid of bugs.
After reading Peter Norvig's Paradigms of Artificial Intelligence ...
107
votes
14
answers
13k
views
Does TDD make defensive programming redundant?
Today I had an interesting discussion with a colleague.
I am a defensive programmer. I believe that the rule "a class must ensure that its objects have a valid state when interacted with from outside ...
0
votes
3
answers
156
views
Defensive coding against an 'official' WSDL service specification
Earlier today I was asked to diagnose an issue in some development code. It turned out that the issue was caused by a new stub implementation returning random data which did not match the service ...
118
votes
16
answers
24k
views
Should I add redundant code now just in case it may be needed in the future?
Rightly or wrongly, I'm currently of the belief that I should always try to make my code as robust as possible, even if this means adding in redundant code / checks that I know won't be of any use ...
4
votes
1
answer
4k
views
Checking for nil in Go [closed]
In Go, is it idiomatic to check for nil and return an error if a parameter is nil?
Should pointer method receivers ever include nil checks?
I've seen a lot of code in other languages where people ...
2
votes
3
answers
588
views
Null values handling in big scale applications
Tl;dr: Should we return null and not know origins of the error or throw exceptions and handle them appropriately?
A few years ago I found this article: http://stackify.com/golden-rule-programming/
...
31
votes
7
answers
9k
views
Should I validate a method call's return value even if I know that the method can't return bad input?
I'm wondering if I should defend against a method call's return value by validating that they meet my expectations even if I know that the method I'm calling will meet such expectations.
GIVEN
User ...
0
votes
0
answers
253
views
Design by Contract and Defensive Programming Confusion [duplicate]
I have been interested in better coding practices/methods which makes the reliability and maintenance less painful effort. I read the chapter about Design by Contract on "Object Oriented Software ...
1
vote
1
answer
705
views
Your thoughts on Best Practices for Scientific Computing? [closed]
A recent paper by Wilson et al (2014) pointed out 24 Best Practices for scientific programming. It's worth to have a look. I would like to hear opinions about these points from experienced programmers ...
1
vote
1
answer
4k
views
Should we always write Defensive null check in code? [duplicate]
Are there any scenarios where we should not write defensive checks for null?
Should we write defensive code or check for NULL every time we have passed a parameter or received a value back from a ...
3
votes
3
answers
656
views
Is it worthwhile to try to write foolproof data structures?
The problem
We need to store data in a table-like way, but we have very strict space constraints (~1Mb per table of 10k+ rows). We store data like this:
ID | reviews | factor | score | interval | ...
27
votes
2
answers
11k
views
Differences between Design by Contract and Defensive Programming
Could Designing by Contract (DbC) be a way to program defensively?
Is one way of programming better in some cases than the other?
45
votes
10
answers
41k
views
What defines robust code?
My professor keeps referring to this Java example when he speaks of "robust" code:
if (var == true) {
...
} else if (var == false) {
...
} else {
...
}
He claims that "robust code" means ...
12
votes
3
answers
640
views
Do I need to deal with the situation where private methods are called through reflection?
When creating a library, must I ensure that the private methods must work as expected when called not by other methods of the same class, but by another library through reflection?
For example, if a ...
12
votes
4
answers
1k
views
How defensive should we be?
We've been running Pex over some code, and it has been showing some good things (well bad things, but showing them before it gets to production!).
However, one of the nice things about Pex is that it ...
56
votes
5
answers
22k
views
if ('constant' == $variable) vs. if ($variable == 'constant')
Lately, I've been working a lot in PHP and specifically within the WordPress framework. I'm noticing a lot of code in the form of:
if ( 1 == $options['postlink'] )
Where I would have expected to see:
...