Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defensive programming [closed]

When writing code do you consciously program defensively to ensure high program quality and to avoid the possibility of your code being exploited maliciously, e.g. through buffer overflow exploits or code injection ?

What's the "minimum" level of quality you'll always apply to your code ?

like image 626
David Avatar asked Aug 09 '08 17:08

David


People also ask

What is meant by defensive programming?

Defensive programming is an approach wherein the programmer assumes he is capable of mistakes, and therefore can apply the proper practices to produce higher-quality code.

What is defensive programming example?

Defensive programming is the practice of writing software to enable continuous operation after and while experiencing unplanned issues. A simple example is checking for NULL after calling malloc() , and ensuring that the program gracefully handles the case.

Why is defensive programming essential?

Defensive programming can be tough to write source code, but it results in high-quality foolproof code. Without Defensive programming, your code will still run normally. However, it can easily break or give incorrect output depending on the condition or user input.

What is intent in defensive programming?

Defensive programming, simply put, is programming with the intent to anticipate likely failure points. The goal is to circumvent those likely problems before they occur.


2 Answers

In my line of work, our code has to be top quality.
So, we focus on two main things:

  1. Testing
  2. Code reviews

Those bring home the money.

like image 181
abyx Avatar answered Sep 21 '22 15:09

abyx


Similar to abyx, in the team I am on developers always use unit testing and code reviews. In addition to that, I also aim to make sure that I don't incorporate code that people may use - I tend to write code only for the basic set of methods required for the object at hand to function as has been spec'd out. I've found that incorporating methods that may never be used, but provide functionality can unintentionally introduce a "backdoor" or unintended/unanticipated use into the system.

It's much easier to go back later and introduce methods, attributes, and properties for which are asked versus anticipating something that may never come.

like image 37
Tom Avatar answered Sep 21 '22 15:09

Tom