Defensive Programming

Design by contract

A compromise of the functionalities and behavior of a module;

Use assert()


  • improve testing
  • detect bugs
  • behaves like executable comments
  • improve code quality
  • can be turned on and off


  • slowdown execution
  • commonly misunderstood
  • some times used improperly for error handling


Static Code Analysis

Excelent tool for detecting errors that the compiler won’t detect.

Other techniques

Data hiding (encapsulation)

exposes only necessary functions;

Robust watchdogs

  • should have separated clock sources;
  • fast detection of failures
  • ability to accuse what went wrong

Stack monitor

easily implemented in RTOS, but hard to implement in baremetal;

Error handling



Benigno Defensive Programming


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s