Home > Articles > Programming

A First Taste of Design by Contract

  • Print
  • + Share This
In this sample chapter, authors Jim McKim and Richard Mitchell provide a quick introduction to design by contract. Learn how contracts can specify the required behavior of a class and check the code at runtime; some of the benefits of design by contract; and more.
This sample chapter is excerpted from Design by Contract, by Example, by Jim McKim and Richard Mitchell.
This chapter is from the book

This chapter is from the book

1.1 About This Chapter

This chapter provides a quick introduction to design by contract. In doing so, it

  • Shows you how contracts can specify the required behavior of a class, and how contracts can check the code at runtime.

  • Explains that contracts are built of assertions, which are used to express preconditions, postconditions, and invariants. (Later chapters develop a small set of principles and guidelines to help you write high-quality contracts.)

  • Provides an example that is a simplified version of what could be a real software component—a customer manager—but you don't need any experience with components to understand the example.

  • Presents some of the benefits of design by contract, a theme that is followed up in more detail in Chapter 8.

  • Offers a first taste of design by contract. It'll probably raise many questions in your mind. We haven't attempted to answer them all in Chapter 1. We hope the rest of the book will answer many of them.

Throughout this book we use the Unified Modeling Language (UML) for diagrams that summarize classes and the programming language Eiffel for writing contracts and implementations. (Chapter 11 presents two examples in Java.) This book does not teach UML, Eiffel, or Java. We just explain the bits we need as we go along. The bibliography lists a small selection of books, papers, and Web sites where you can get more information.

  • + Share This
  • 🔖 Save To Your Account