Skip to main content

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.

Filter by
Sorted by
Tagged with
2 votes
1 answer
347 views

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'...
Joel Gibson's user avatar
-2 votes
1 answer
81 views

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 ...
TheAdmin's user avatar
1 vote
3 answers
370 views

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 ...
kai's user avatar
  • 245
3 votes
4 answers
338 views

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 ...
Michael Ozeryansky's user avatar
1 vote
5 answers
1k views

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 ...
Lefteris008's user avatar
2 votes
1 answer
774 views

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 ...
user avatar
0 votes
1 answer
65 views

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 ...
user avatar
3 votes
3 answers
207 views

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 ...
H2CO3's user avatar
  • 437
1 vote
1 answer
546 views

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 ...
user58446's user avatar
  • 327
2 votes
2 answers
1k views

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 ...
Grad student's user avatar
5 votes
3 answers
4k views

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 ...
fr_andres's user avatar
  • 171
107 votes
14 answers
13k views

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 ...
user2180613's user avatar
  • 1,792
0 votes
3 answers
156 views

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 ...
Kinexus's user avatar
  • 303
118 votes
16 answers
24k views

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 ...
KidCode's user avatar
  • 2,213
4 votes
1 answer
4k views

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 ...
sqroot's user avatar
  • 49
2 votes
3 answers
588 views

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/ ...
Dovydas Navickas's user avatar
31 votes
7 answers
9k views

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 ...
Didier A.'s user avatar
  • 1,397
0 votes
0 answers
253 views

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 ...
Deniz's user avatar
  • 109
1 vote
1 answer
705 views

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 ...
Kouichi C. Nakamura's user avatar
1 vote
1 answer
4k views

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 ...
Hemant Kothiyal's user avatar
3 votes
3 answers
656 views

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 | ...
Attila O.'s user avatar
  • 241
27 votes
2 answers
11k views

Could Designing by Contract (DbC) be a way to program defensively? Is one way of programming better in some cases than the other?
Gabriel Fair's user avatar
45 votes
10 answers
41k views

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 ...
Lotus Notes's user avatar
12 votes
3 answers
640 views

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 ...
Arseni Mourzenko's user avatar
12 votes
4 answers
1k views

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 ...
Peter K.'s user avatar
  • 3,818
56 votes
5 answers
22k views

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: ...
Tom Auger's user avatar
  • 857