Defensive  Programming
Summary <ul><li>Defensive at compile time </li></ul><ul><li>Use Static Analysis Tools </li></ul><ul><li>Assertion </li></u...
Failing fast <ul><li>Don ‘ t Work around problems </li></ul><ul><li>Fail immediately and vis i bly </li></ul><ul><li>Prefe...
W arning  as error <ul><li>Turn on all warnings (-Wall) </li></ul><ul><li>Turn on variable argument s  check (-Wformat) </...
Refine define <ul><li>Static cast </li></ul><ul><ul><li>#define FLY(pBird) static_cast<CBird*>(pBird)->Eat() </li></ul></u...
Pclint <ul><li>Variable have not been initialized </li></ul><ul><li>new [] / delete mismatch </li></ul><ul><li>Access out-...
Pclint walker <ul><li>Enhanced Error description (Chinese) </li></ul><ul><li>Only Essential Errors, without errors that co...
Keys of  Assertion <ul><li>D ata invariants  that  ensure data integrity </li></ul><ul><li>Pre- and postconditions that en...
Examples of  Assertion <ul><li>MFC assert_valid </li></ul><ul><li>Default of switch </li></ul><ul><li>Null pointer </li></...
Valgrind <ul><li>Runtime  Analysis  Tool More Precise </li></ul><ul><li>new [] / delete Mismatch </li></ul><ul><li>Access ...
Alpha Version <ul><li>Enable Core and Symbol </li></ul><ul><li>-fstack-protector-all(>gcc4.1) </li></ul><ul><li>Enable Ass...
Reference <ul><li>Code complete  </li></ul><ul><ul><li>C hapter 8 defensive programming </li></ul></ul><ul><li>Code craft ...
Upcoming SlideShare
Loading in …5
×

Defensive programming

535 views

Published on

static analysis, assertion, dynamic analysis, alpha version

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
535
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Defensive programming

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

×