Defensive  Programming
Summary Defensive at compile time Use Static Analysis Tools Assertion Use  Runtime  Analysis Tools Alpha Version
Failing fast Don ‘ t Work around problems Fail immediately and vis i bly Prefer compile err to runtime err Dead Programs Tell No Lies
W arning  as error Turn on all warnings (-Wall) Turn on variable argument s  check (-Wformat) Treat warning as error (-Werror) Use -isystem to ignore warnings
Refine define Static cast #define FLY(pBird) static_cast<CBird*>(pBird)->Eat() So FLY(pDuck); will fail Do while(0) : limit the scope of variables in define
Pclint Variable have not been initialized new [] / delete mismatch Access out-of-bounds
Pclint walker Enhanced Error description (Chinese) Only Essential Errors, without errors that compiler can check. Automatically and recursively check all project files Easily Integrated to tools (e.g. Jenkins)
Keys of  Assertion D ata invariants  that  ensure data integrity Pre- and postconditions that ensure function correctness No  Side effect Enable In  Alpha and  D ebug  Version R edirect  o utput  on Linux, or log to file
Examples of  Assertion MFC assert_valid Default of switch Null pointer Index of array u32 defer u8 memory in our Sybase SDK
Valgrind Runtime  Analysis  Tool More Precise new [] / delete Mismatch Access out-of-bounds Uninitialized  Memory
Alpha Version Enable Core and Symbol -fstack-protector-all(>gcc4.1) Enable Assert, and Crash ASAP Enable Optimize Same As Release Version Enable Runtime  Analysis  Tool(eg. TCMalloc heapcheck)
Reference Code complete  C hapter 8 defensive programming Code craft Chapter 1 on the defensive

Defensive programming

  • 1.
  • 2.
    Summary Defensive atcompile time Use Static Analysis Tools Assertion Use Runtime Analysis Tools Alpha Version
  • 3.
    Failing fast Don‘ t Work around problems Fail immediately and vis i bly Prefer compile err to runtime err Dead Programs Tell No Lies
  • 4.
    W arning as error Turn on all warnings (-Wall) Turn on variable argument s check (-Wformat) Treat warning as error (-Werror) Use -isystem to ignore warnings
  • 5.
    Refine define Staticcast #define FLY(pBird) static_cast<CBird*>(pBird)->Eat() So FLY(pDuck); will fail Do while(0) : limit the scope of variables in define
  • 6.
    Pclint Variable havenot been initialized new [] / delete mismatch Access out-of-bounds
  • 7.
    Pclint walker EnhancedError description (Chinese) Only Essential Errors, without errors that compiler can check. Automatically and recursively check all project files Easily Integrated to tools (e.g. Jenkins)
  • 8.
    Keys of Assertion D ata invariants that ensure data integrity Pre- and postconditions that ensure function correctness No Side effect Enable In Alpha and D ebug Version R edirect o utput on Linux, or log to file
  • 9.
    Examples of Assertion MFC assert_valid Default of switch Null pointer Index of array u32 defer u8 memory in our Sybase SDK
  • 10.
    Valgrind Runtime Analysis Tool More Precise new [] / delete Mismatch Access out-of-bounds Uninitialized Memory
  • 11.
    Alpha Version EnableCore and Symbol -fstack-protector-all(>gcc4.1) Enable Assert, and Crash ASAP Enable Optimize Same As Release Version Enable Runtime Analysis Tool(eg. TCMalloc heapcheck)
  • 12.
    Reference Code complete C hapter 8 defensive programming Code craft Chapter 1 on the defensive