Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

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 );
 // ...
}


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint