Examples
Team LiB
Previous Section Next Section

Examples

Example: Do assert on basic assumptions. We all have war stories about assertions that "couldn't possibly fire," but did. Time and again, it's: "This value is certainly positive!" "That pointer is obviously not null!" Do recheck tautologies: Software development is complex, change is the norm, and anything can happen in a program that is changing. Assertions verify that what you believe is "obviously true" today actually stays true. Do assert on tautologies that the type system cannot enforce:



string Date::DayOfWeek() const {


  assert( day_ > 0 && day_ <= 31 );               // invariant checks


  assert( month_ > 0 && month_ <= 12 );


  // …


}



    Team LiB
    Previous Section Next Section