Click here to Skip to main content
15,913,773 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Don't usually share youtube stuff... Pin
OriginalGriff3-Nov-15 2:24
mveOriginalGriff3-Nov-15 2:24 
GeneralRe: Don't usually share youtube stuff... Pin
glennPattonWork33-Nov-15 2:34
professionalglennPattonWork33-Nov-15 2:34 
GeneralRe: Don't usually share youtube stuff... Pin
dexterama3-Nov-15 4:31
professionaldexterama3-Nov-15 4:31 
GeneralWhat is meant by Defensive Programming? Pin
Amarnath S3-Nov-15 0:50
professionalAmarnath S3-Nov-15 0:50 
GeneralRe: What is meant by Defensive Programming? PinPopular
Nagy Vilmos3-Nov-15 0:58
professionalNagy Vilmos3-Nov-15 0:58 
AnswerRe: What is meant by Defensive Programming? Pin
Duncan Edwards Jones3-Nov-15 1:02
professionalDuncan Edwards Jones3-Nov-15 1:02 
GeneralRe: What is meant by Defensive Programming? Pin
Weylyn Cadwell3-Nov-15 2:00
Weylyn Cadwell3-Nov-15 2:00 
GeneralRe: What is meant by Defensive Programming? Pin
Maximilien3-Nov-15 1:07
Maximilien3-Nov-15 1:07 
"Defensive programming is a form of defensive design intended to ensure the continuing function of a piece of software under unforeseen circumstances. The idea can be viewed as reducing or eliminating the prospect of Finagle's law having effect" [^]

IMO, this goes with design by contract[^] programming. (pre and post conditions)

In short, you make your own code fool proof according to the design and architecture (and contract) of the existing code.

BAD EXAMPLE BELOW ... DO NOT USED... I keep it there just because I suck this morning.

As a (very simplified example) (in c-ish language)

If the design (contract) say that p can be invalid, then you need to handle that case.
void f ( SomeClass* p )
{
   p->doSomething();
}

will crash is p is invalid.

defensive programming will have something like:
C++
void f ( SomeClass* p )
{
  if (p)
   p->doSomething();
}

or
C++
void f ( SomeClass* p )
{
   if ( invalid(p))
   {
      assert_and_log_message("invalid p in function f");
      return; 
   }
   p->doSomething();
}

In both case, your own code will not crash.

I'd rather be phishing!


modified 3-Nov-15 8:48am.

GeneralRe: What is meant by Defensive Programming? Pin
Richard Deeming3-Nov-15 2:26
mveRichard Deeming3-Nov-15 2:26 
GeneralRe: What is meant by Defensive Programming? Pin
Maximilien3-Nov-15 2:47
Maximilien3-Nov-15 2:47 
GeneralRe: What is meant by Defensive Programming? Pin
kdmote4-Nov-15 5:05
kdmote4-Nov-15 5:05 
AnswerRe: What is meant by Defensive Programming? Pin
Super Lloyd3-Nov-15 1:21
Super Lloyd3-Nov-15 1:21 
GeneralRe: What is meant by Defensive Programming? Pin
Wynter Dragon4-Nov-15 4:16
Wynter Dragon4-Nov-15 4:16 
GeneralRe: What is meant by Defensive Programming? Pin
harold aptroot3-Nov-15 2:15
harold aptroot3-Nov-15 2:15 
GeneralRe: What is meant by Defensive Programming? PinPopular
W Balboos, GHB3-Nov-15 2:16
W Balboos, GHB3-Nov-15 2:16 
GeneralRe: What is meant by Defensive Programming? Pin
Marc Clifton3-Nov-15 2:22
mvaMarc Clifton3-Nov-15 2:22 
GeneralRe: What is meant by Defensive Programming? Pin
OriginalGriff3-Nov-15 2:29
mveOriginalGriff3-Nov-15 2:29 
GeneralRe: What is meant by Defensive Programming? Pin
Munchies_Matt3-Nov-15 2:39
Munchies_Matt3-Nov-15 2:39 
GeneralRe: What is meant by Defensive Programming? Pin
Eytukan3-Nov-15 3:24
Eytukan3-Nov-15 3:24 
GeneralRe: What is meant by Defensive Programming? Pin
BillWoodruff3-Nov-15 5:01
professionalBillWoodruff3-Nov-15 5:01 
GeneralRe: What is meant by Defensive Programming? Pin
PIEBALDconsult3-Nov-15 12:46
mvePIEBALDconsult3-Nov-15 12:46 
GeneralRe: What is meant by Defensive Programming? Pin
zandam3-Nov-15 22:35
zandam3-Nov-15 22:35 
GeneralRe: What is meant by Defensive Programming? Pin
Mycroft Holmes3-Nov-15 13:52
professionalMycroft Holmes3-Nov-15 13:52 
GeneralRe: What is meant by Defensive Programming? Pin
R. Erasmus3-Nov-15 23:29
R. Erasmus3-Nov-15 23:29 
GeneralRe: What is meant by Defensive Programming? Pin
Kirk 103898214-Nov-15 3:05
Kirk 103898214-Nov-15 3:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.