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
311
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
68
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
343
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
313
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
753
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
53
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
180
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
535
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
138
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
3k
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 ...