Testing on the Toilet

May 1, 2008

Testable Contracts Make Exceptional Neighbors Consider the following function, which modifies a client-supplied object: bool SomeCollection::GetObjects(vector* objects) const { objects->clear(); typedef vector::const_iterator Iterator; for (Iterator i = collection_.begin(); i != collection_.end(); ++i) { if ((*i)->IsFubarred()) return false; objects->push_back(*i); } return true; }

Consider when GetObjects() is called. What if the caller doesn't check the return value, and assumes the data is in a valid state when it actually is not? If the caller does check the return value, what can it assume about the state of its objects in the failure case? When GetObjects() fails, it would be much better if either all the objects were collected or none of them. This can help avoid introducing hard to find bugs. By using good design contracts and a solid implementation, it is reasonably easy to make functions like GetObjects() behave like transactions. By following Sutter's rule of modifying externally-visible state only after completing all operations which could possibly fail [1], and mixing in Meyers's “swap trick” [2], we move from the realm of undefined behavior to what Abrahams defines as the strong guarantee [3]: bool SomeCollection::GetObjects(vector* objects) const { vector known_good_objects; typedef vector::const_iterator Iterator; for (Iterator i = collection_.begin(); i != collection_.end(); ++i) { if ((*i)->IsFubarred()) return false; known_good_objects->push_back(*i); } objects->swap(known_good_objects); return true; }

At the cost of one temporary and a pointer swap, we've strengthened the contract of our interface such that, at best, the caller received a complete, new collection of valid objects; at worst, the state of the caller's objects remains unchanged. The caller might not verify the return value, but will not suffer from undefined results. This allows us to reason much more clearly about the program state, making it much easier to verify the intended outcome with automated tests as well as recreate, pinpoint, and banish bugs with regression tests. 1 http://www.gotw.ca/publications 2 Scott Meyers, Effective C++ 3 http://www.boost.org/more/generic_exception_safety.html

More information, discussion, and archives: http://googletesting.blogspot.com Copyright © 2007 Google, Inc. Licensed under a Creative Commons Attribution–ShareAlike 2.5 License (http://creativecommons.org/licenses/by-sa/2.5/).

Testable Contracts Make Exceptional Neighbors Code

May 1, 2008 - ... we've strengthened the contract of our interface such that, at best, ... Copyright © 2007 Google, Inc. Licensed under a Creative Commons.

211KB Sizes 0 Downloads 159 Views

Recommend Documents

On verifying resource contracts using Code Contracts
languages), this problem gets even more complex since memory consumption depends on the behavior ... We present an extension of the CODE CONTRACTS annotation language designed to specify the ...... [2] Wolfgang Ahrendt, Thomas Baar, Bernhard Beckert,

Code of Practice for Communications Service Contracts
Feb 2, 2010 - 2.3 If a service provider chooses to pledge compliance, it may pledge compliance in respect of (i) all existing contracts or specified existing.

Code of Practice for Communications Service Contracts
Feb 2, 2010 - (a) application for communications services on the terms and ... (c) the name of the company which the customer is contracting with in respect of ...

Is Intertemporal Choice Theory Testable?
a Kreps–Porteus style utility function over an infinite horizon consumption program. .... The resulting function is clearly concave and strictly increasing and the.

Code of Practice for Service Contracts - Communications Authority
Feb 2, 2010 - customer, including services provided after free-trial period (subject to the ..... email, online download or collection at shop at the choice of the.

School Choice with Neighbors
got from the audience during presentations at Conference on Economic ..... Call ¯. K{a,h(a)} student a's joint preference relation. In order to keep the notation.

Cognitive Scientists Prefer Theories and Testable ...
ogy, computer science, linguistics, anthropology discourse processing .... the planet to run a between-subjects design with 4 variables on 4 dimensions. Tests of ...

the neighbors s02e12.pdf
season 2 soundtrack list 2013 the neighbors. The mysteries of laura. s02e12 hdtv x264 lol 720p hdtv x265 mega. Download the neighbors 2012 s02 complete ...

Escondido Neighbors United opposition to library outsourcing ...
Escondido Neighbors United opposition to library outsourcing 11AUG2017.pdf. Escondido Neighbors United opposition to library outsourcing 11AUG2017.pdf.

School Choice with Neighbors
choice is school s2 together. Otherwise each student cares only about his own school: student a1 ranks s1 over s2 over the outside option, and student a2 ranks s2 over the outside option over s1. Note that even though student a1 prefers s1 over s2 if

School Choice with Neighbors
got from the audience during presentations at Conference on Economic .... follows that the student-proposing DA mechanism is not stable in this setting, ..... Call ¯. Ka student a's joint preference relation. A student a need not have the same.

city neighbors charter school -
continue next week and we ask that families do their best to maintain the ... the stage tech for this performance and discussion specifically for middle school ...

Reporting Neighbors in High-Dimensional Euclidean Space
(c) For each cell τ with |Pτ | ≥ 2, go over all pairs of points of Pτ and report those pairs at ...... Geometry, Second Edition, CRC Press LLC, Boca Raton, FL, 2004.

city neighbors charter school -
(add one day). NEXT BUILDING WORK DAY SATURDAY MARCH 17. 8am-12 pm Spring Cleaning and Spring Fixing. Stan Brown, Director of Facilities will be here with his. Building Committee Co-Captains. All Welcome to Help! Please bring tools: drills, drivers,