Question

Possible Duplicate:
Favorite (Clever) Defensive Programming Best Practices

I am always advised by some programmers to pay concentration to easy debugging. What is defensive programming and to which extend should it be considered while practicing?

And one more important question: is there any key things to consider while coding and what are they?

Was it helpful?

Solution

Have a look at

Defensive programming is the idea that the developer makes as few assumptions as absolutely necessary. In addition, the developer preemptively creates code that anticipates not only potential problems but also specification changes.

OTHER TIPS

As a rule of thumb -- if you catch yourself thinking "this will always be true", write ASSERT( condition) in that place. That is probably the core of what defensive programming should be ;).

If defensive programming meant only one thing , that should be use assert extensively.

Here is a good article about when and where to use assert.

There are many situations where it is good to use assertions. This section covers some of them:

* Internal Invariants
* Control-Flow Invariants
* Preconditions, Postconditions, and Class Invariants

http://en.wikipedia.org/wiki/Defensive_programming

Defensive programming means, that you check if a file exists and if you have the permissions to open it instead of just trying to open it and catching any eventual exceptions. (Just an example)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top