Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Automating the Development Process


July 2000 Feature: Automating the Development Process

Broken down, the latest development trend–Design by Contract–is hardly a new concept. Some modern languages, such as Eiffel, make Design by Contract an explicit part of the language. In fact, Design by Contract originated in Eiffel. Eiffel classes are components that cooperate through the use of the contract, which defines the obligations and benefits for each class.

While Design by Contract may not yet be a part of most widely-used programming languages, such as C, C++ and Java, it should be. After all, any piece of code has implicit contracts attached to it. The simplest example is the dreaded NullPointerException in Java. If we go to the developer who wrote the code and show him the problem, he will probably say, "Of course a NullPointer Exception happens! You should never pass a ‘null’ to that method."

Simply put, Design by Contract is a structured way of writing comments to define what code should do. The contract requires components of the code (such as classes or methods) to follow certain specifications as they interact with each other. The interactions between these components must fulfill a set of predetermined mutual obligations.

Indeed, Design by Contract techniques can easily be applied to virtually any language if we use the necessary tools. Design by Contract is being used in a modern language, and many developers see it as a useful concept. But, as is the case with most new techniques, there is resistance to any new innovation that takes time to implement and requires changes in the way we develop.

Fortunately, the time and changes here are minimal. Using Design by Contract requires the person who writes the code to document the contracts assumed in the code and to use tools to ensure that the contracts are written and enforced correctly. There are simple enough ways to implement Design by Contract, and the benefits to a development team are many. Furthermore, Design by Contract is a key step towards automating the development process.

Readable Specifications

The first major benefit of Design by Contract is quite obvious. If you use Design By Contract techniques, you’ll have the specifications of the code sitting right in the code in a readable format. This solves a key problem in the way developers typically write code.

When most developers write code, that’s all they do. The original specifications are lost because the developer does not include them in the source. Someone who examines the code months later will have to decipher it and then backtrack to figure out what the specifications were. The next step will be to determine whether the code met these presumed specifications. Rather than go through all of these steps, the new developer will likely just use the code and trust that it will work, but he will never really know whether it met the original specifications.

Old code without comments is the worst-case scenario, of course. Comments give us some hope because they explain different segments of the code. But there can be major differences in commenting style. The developer who is reusing the code months later will probably assume that the original developer commented the code properly. This may be a big assumption. Developers nowadays come from all across the country and all over the world. Differences in training, native language and writing skills may cause two developers to describe a line of code using two completely different phrases. Under these circumstances, comments may actually confuse more than they clarify. They waste the time of the second developer, and they offer no guarantee of proper interpretation.

A Paper Trail

The second major benefit of Design by Contract is related to the state of the job market. With such frequent turnover in our current software development workforce, the importance of providing clear information about our code is at an all-time high. When the lead developer on a project moves on to his next job–after diligently serving a record eight months at your company–you don’t want to be stranded without complete knowledge of what his code should do. Clearly, commenting the code isn’t enough; we need a way of standardizing our comments.

Design by Contract essentially serves as a set of coding standards for code comments. If your company sets these standards in place, you can rest assured that new developers who come to your company will only need to learn Design by Contract in order to analyze code and determine whether it meets its specifications. In this sense, the arguments in favor of using Design by Contract are the same as the arguments for using naming conventions. Code shouldn’t be useful to just one developer; it should be useful to endless generations of employees.

The third benefit of Design by Contract lies in its ability to bridge the gap between software design and functionality testing. In other words, Design by Contract automates a piece of development that nobody thought could be automated: functionality testing. It has been thought that functionality testing requires human interaction because a tool can’t possibly determine whether a piece of code did what a human being wanted it to do. However, if we have a full understanding of what we want the code to do and can explain it using a standardized system of comments, there is no reason we can’t automate functionality testing.

Overcoming Resistance

The benefits of Design by Contract are fairly clear, but there is that lingering obstacle of human resistance. How difficult is it to implement Design by Contract in Java development? Not very. There have been several efforts to make Design by Contract available in Java. Most of the efforts involve using Javadoc comments to specify the contracts. A simple example is:

/**

* @invariant getCost() >= 0

*/

public class ShoppingCart

{

/**

* @pre item != null

* @post result > 0

*/

public float add (Item item) {

_items.addElement (item);

_cost += item.getPrice ();

return _cost;

}

private float _cost = 0;

private Vector _items = new Vector ();

}

In the above example, you can see that the class contract is specified using the @pre (precondition), @post (postcondition) and @invariant (class invariants) tags.

Preconditions are conditions that the client of the class needs to satisfy in order for the class to work properly. They outline the restrictions on arguments and specify what values the arguments can’t take.

Postconditions are conditions that the implementor or supplier of the class will always fulfill as long as the client has fulfilled the preconditions. Class invariants are conditions that are guaranteed always to be true. Postconditions also specify what the return values should be.

Note that the concept of Design by Contract is similar to assertions, but quite a bit more sophisticated and useful. A piece of code containing explicit contracts has several advantages over a similar piece of code without explicit contracts:

• First of all, the code’s assumptions are clearly documented (for example, you assume that "item" should not be "null") and the contract documentation is guaranteed to be up to date. Design concepts are placed directly in the code itself.

• Second, the code’s contracts can be checked for consistency because they are explicit.

• Third, the class implementation can assume that input arguments satisfy the preconditions, so the implementation can be simpler and more efficient.

• Fourth, the class client is guaranteed that the results will satisfy the postconditions.

• Finally, the code is much easier to reuse.

In short, Design by Contract allows development teams to produce much more robust and reliable software. Many software design errors that would otherwise go unnoticed become easy to catch during any stage of development.

Design errors can be caught by the developer while he or she is writing or thinking about the specification of the contract. Because the developer is looking at the specifications and the code at the same time, mistakes will be obvious. Here is where Design by Contract eliminates an obstacle to error prevention: With code and specifications side-by-side, it takes less work to make sure that the code follows the specifications, and developers will be less likely to revolt against implementation of the process.

Design by Contract also allows developers to catch design flaws at run time while the contracts are being checked. Because the comments are now standardized, tools can actually understand the contract and enforce it, essentially performing automatic black-box testing. Tools can automatically enforce that the contract describes all of the arguments and all of the returned values. They can also enforce that as the developer is writing the code, every input and output must be described in the contract.

Design errors can also be caught by testing tools, which can use the contract information to test the classes more thoroughly and efficiently. It is a matter of common sense that tools will be more efficient when they know exactly what they are looking for.

Making the Implicit Explicit

Design by Contract allows us to reap many benefits with relatively little effort. The only effort we invest is that which is required for us to write the implicit contract explicitly. For the above benefits to become available, however, the code and clients must satisfy the explicit contract. This is where tools come into play. To maximize our benefit and minimize our investment of time, we should use the following types of tools:

• Syntax and semantic checker: a tool that checks the syntax and validity of each contract.

• Run time checker: a tool that checks at run time to make sure that all contracts have been satisfied, methods are called according to the contract, and results generated also satisfy the contract.

• Contract verifier: a tool that checks that the class implementation has satisfied its contract and the contract of any classes it calls.

Existing tools do help the Design by Contract process, but there is a need for additional tools. If a tool existed that could output a contract from design tools, we could automate everything from design to testing. In this sense, Design by Contract fits well into our vision of a fully-automated development process. Consider how far along we are in this goal: The computer already performs many of the key tasks in development, such as white-box testing, syntax-checking and debugging.

Think about it: The only true intelligence creation in the software development process occurs when a high-level designer lays out the specifications and designs the algorithms for a new application. There are already CASE tools available that can automate implementation by generating code. Although these tools have understandable limitations, we expect them to improve just as any technology improves with research.

The testing phase, though vital to overall software quality, thus contains no intelligence creation because the intelligence needed to test is already in the code. Automatic tools can enforce coding standards, perform unit testing, and detect run-time errors. They can also generate inputs to perform white-box testing. The remaining obstacles to fully-automated testing are black-box and usability testing because people assume these types of testing require human intervention. However, Design by Contract should be able to automate any functionality testing for developers who truly understand the functionality of their application and can write about it in their contract.

For all of these reasons, Design by Contract is an appealing technique. Its benefits apply directly to what our developers are doing now, but it is also a vehicle by which we will soon be able to automate an even greater portion of the software development cycle.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.