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.

Defensive programming is a style of programming designed to minimize the possibility of bugs introduced by code changes or unforeseen usage of the software. Examples of this style include "yoda" conditions where the constant is placed before the variable being tested if (42 == foo) to cause compile time errors if the assignment equals was used rather than the comparison form. Another example is to always having braces around a block (e.g. single statement for loops) to reduce the possibility of a later code addition forgetting to add them and causing the statement to be in the wrong scope.

26 questions
118
votes
16 answers

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 right now, but they might be x amount of years down…
107
votes
14 answers

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 the class" must always be adhered to. The reason…
user2180613
  • 1,752
  • 3
  • 14
  • 17
55
votes
5 answers

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: if ( $options['postlink'] == 1 ) Is this a…
45
votes
10 answers

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 that your program takes into account all…
Lotus Notes
  • 553
  • 1
  • 5
  • 7
31
votes
7 answers

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 getUser(Int id) { User temp = new User(id); …
Didier A.
  • 1,327
  • 14
  • 10
27
votes
2 answers

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?
Gabriel Fair
  • 373
  • 1
  • 3
  • 8
12
votes
4 answers

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 doesn't necessarily stop trying to find…
Peter K.
  • 3,828
  • 1
  • 23
  • 34
12
votes
3 answers

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 private method private DoSomething(int number)…
Arseni Mourzenko
  • 134,780
  • 31
  • 343
  • 513
5
votes
3 answers

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 Programming: Case Studies in Common LISP I was not…
4
votes
1 answer

Checking for nil in Go

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 applying defensive programming also checked for nulls…
sqroot
  • 49
  • 1
  • 2
3
votes
4 answers

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 value and immediately return nil if it's nil, or…
3
votes
3 answers

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 connection in Mbps Length of a swimming pool in…
3
votes
3 answers

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 | etc. ---+---------+--------+-------+----------+----- …
Attila O.
  • 241
  • 2
  • 11
2
votes
1 answer

Is it possible to restructure code to avoid this copy-paste bug?

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've reduced it to the the example below. static…
Joel Gibson
  • 131
  • 4
2
votes
1 answer

Is using the copy constructor in an object's construction is bad?

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 set of authors. class Book { private String…
user313955
1
2