SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Design by Contract is a systematic approach to specifying and implementing object-oriented software elements based on precisely defined obligations between them. This is the first practical, example-centered guide to using Design by Contract. The authors introduce powerful design principles for writing good contracts and supporting guidelines -- and demonstrate those principles with real-world Java and Eiffel code examples. They review the fundamentals of Design by Contract -- preconditions, postconditions and invariants -- and presenting a real-world example. Next, drawing on their unparalleled experience with Design by Contract, they present six powerful design principles -- each with a clear statement of goals, rationale, examples in both Java and Eiffel, and supporting notes. Developers will learn how to strengthen their components' internal support for contracts; how to use contracts to specify subclasses whose objects can safely be substituted for superclass objects; how to extend contract to specify and check properties that remain unchanged; and how to use contracts in analysis-level models. The book concludes by summarizing key principles and presenting a realistic cost/benefit analysis of their use.
A First Taste of Design by Contract
Click below for Sample Chapter related to this title:
mitchellch01.pdf
Foreword.
Preface.
1. A First Taste of Design by Contract.
About This Chapter.
The Customer Manager Example.
Some Questions.
A Contract for CUSTOMER_MANAGER.
The Story So Far.
Runtime Checking.
Trustworthy Documentation.
Summary.
An Aide Memoire.
Things to Do.
About This Chapter.
Stacks.
Separate Commands and Queries.
Naming Conventions.
Separate Basic Queries and Derived Queries.
Specify How Commands Affect Basic Queries.
Capture Unchanging Properties in Invariants.
The Class and Its Contract.
The Basic Queries Are a Conceptual Model of Stacks.
The Six Principles.
Things to Do.
About This Chapter.
Dictionaries.
Separating and Categorizing Features.
Postconditions.
Preconditions.
Invariant.
A Complete, Contract-Level View of DICTIONARY.
Summary.
Things to Do.
About This Chapter.
Support for Linear Structures.
Contracts Involve Expressions.
Immutable Lists.
A Contract for Immutable Lists.
The Basic Queries.
The Creation Command.
The Derived Query Count.
The Derived Query Preceded_by.
The Derived Query Item.
The Derived Query is_equal.
The Derived Query Sublist.
Summary.
Things to Do.
About This Chapter.
Queues.
A Contract for the Remove Feature.
Making Count a Derived Feature.
A Contract for the Initialize Feature.
A Contract for the Head Feature.
A Contract for the put Feature.
More Derived Queries.
Summary.
Things to Do.
About This Chapter.
Superclasses and Subclasses.
Redefining Contracts.
Eiffel Syntax.
Summary.
Invariants and Inheritance.
Designing Superclasses with Guarded Postconditions.
Two Kinds of Inheritance.
Summary.
Things to Do.
About This Chapter.
Change Specifications and Frame Rules.
Frame Rules for put Using Immutable Lists.
Frame Rules for put Using “Forall”.
Kinds of Frame Rules.
Things to Do.
Appendix: More About the Preprocessor.
About This Chapter.
Kinds of Benefits.
Better Designs.
Improved Reliability.
Better Documentation.
Easier Debugging.
Support for Reuse.
Design by Contract and Defensive Programming.
Defending a Program Against Unwanted Input.
Bulletproofing a Routine.
Defensive Programming.
Some Costs and Limitations of Contracts.
About This Chapter.
The Observer Framework.
Immutable Sets.
Attaching and Detaching Observers.
Notification (For One Observer).
Notification (For All Observers).
A Performance Issue.
Frame Rules.
Privacy.
Things to Do.
About This Chapter.
The Examples.
Fulfilling and Testing a Precondition.
Testing Versus Checking.
A Simple Counter Class.
The User's View of the Program.
The Internal Structure of the Program.
The Program's Behavior.
A Minor Detail.
Summary.
Things to Do.
About This Chapter.
Why Java?
Queues.
The Basic Query size().
The Basic Query get().
The Derived Query head().
The Derived Query isEmpty().
The Derived Query shallowCopy().
The Constructor Queue.
The Command put.
The Command remove.
Summary.
Dictionaries.
Names.
The Invariant.
The Basic Queries.
A Derived Query.
The Commands.
The Constructor.
A Possible Set of Classes.
Java Without iContract.
Precondition Testing.
Things to Do.
About This Chapter.
A Use Case.
Contracts in Analysis Models.
A Contract for the withdrawCash Use Case.
From Analysis to Design.
Problem Domain and System Models.
The Object Constraint Language.
Summary.
WHAT THE BOOK COVERS
Design by contract is all about adding assertions to object-oriented programs, at the design and coding stages. Assertions are facts about a program that must be true for the program to be bug-free. The key assertions in design by contract define preconditions, postconditions, and invariants:
The assertions are written in a programming language, so that
This book concentrates on showing you how to write good contracts. The book presents six principles for writing good contracts, and some supporting guidelines. Through examples, the book motivates the principles and guidelines and shows them in use.
After studying the first three chapters, you will be in a position to write high-quality contracts. The rest of the book will help you do even better.
In addition to chapters that develop contracts for individual example classes, there are chapters on contracts in relation to inheritance and on the topic of frame rules (contracts that assert what does not change). Two larger examples towards the end of the book involve developing contracts across more than one class. Chapter 9 concerns the Observer pattern from Gamma et al. 1994, and Chapter 10 presents a small application in which an object in the user interface is shown to respect part of a contract in the heart of the application. Chapter 12 discusses the use of contracts in systems analysis. Chapter 8 reviews the benefits of using contracts and compares design by contract to defensive programming. Chapter 11 explores how to attach contracts to interfaces and explores briefly how you might implement contracts in a distributed environment.
PROGRAMMING LANGUAGESThe examples are presented first in the object-oriented programming language Eiffel. We chose Eiffel for three reasons:
You don't have to be an Eiffel programmer to follow the examples. We are sure you'll be able to carry the principles over to your own programming environment. The issues we raise, and the advice we give, are not specific to Eiffel.
We do rework two of our examples in Java, using a preprocessor (called iContract) that provides support for contracts. This allows us to explore some issues that do not arise so directly in Eiffel and to show you contracts in another language.
WHO THE BOOK IS FORThe book is written for anyone who wants to find out how to write good contracts. We intend it to be useful to practitioners, students (especially the early chapters), teachers, and researchers.
We don't believe the book is one you can curl up with by the fire (in winter) or the pool (in summer) and read your way through. We believe its material has to be studied and, most importantly, tried out. We hope you have access to a programming environment that supports contracts, such as a Java compiler and the iContract tool (see the bibliography for more information) or an Eiffel compiler (again, more information in the bibliography).
We do not teach object-oriented programming. We assume you know how to program in some object-oriented programming language. We have tried to give enough explanation of the Eiffel and Java code that those familiar with other OO languages can follow the examples.
STYLEThe book is based firmly on examples. Usually, a chapter is based on a single example. This means that there is quite a lot of code to wade through at times. However, most of the code is at the level of assertions, which define what a piece of program achieves. This level of code is generally easier to understand than the code that defines how a piece of program achieves its goal. In addition, we usually dissect the code a few lines at a time to make it easier to follow the discussion.
The examples are mostly simple ones. For example, instead of writing full contracts for the customer manager component introduced in Chapter 1, we write them for a look-up table (or dictionary), which is the data structure that underpins the customer manager component. That way, you won't get lost in too many details, and you won't lose sight of the basic principles. Once you see the principles, we are confident you'll be able to apply them to your own, more complicated examples.
We have been selective in what we put into this book. Other books have useful and insightful things to say on the subject of design by contract, but we have concentrated on what makes this book different--the advice it gives on how to write good contracts.
And, of course, this book is not the end of the story of design by contract. There is more work to be done on writing contracts, on developing the underlying technology and the underlying theory, on applying the ideas in broader contexts, and on assessing the benefits in practice.
WEB SITEThere is a Web site associated with the book. It contains the source code of the examples. Our hope is that you will download the code and play with it. Change the code. Add bugs, both in the implementation and in the contracts, and see what happens. Change the examples into new ones. Experiment. Use them on real projects. That's how we learned about contracts.