Contract programming

xiaoxiao2021-03-06  40

The contractual programming contract is a breakthrough technology to reduce the cost of large projects. The contract consists of a prior art, post-test conditions, errors, and invariators. The contract can be added to C and do not need to be transformed into the language, but it is very awkward and inconsistent.

The purpose of supporting the contract within the language is:

Give the contract a consistent viewing tool support to enable the compiler to generate better code from the information collected from the contract, easy to manage and enforce contract handling contract inheritance

The concept of contract is simple - it is just a true expression. If otherwise, the contract is violated, then according to the definition, there must be a bug in the program. The contract constitutes a part of the program specifications, but it is only in the code from the document. As each programmer knows, the document is usually incomplete, outdated, wrong or does not exist. The contract will make the contract to verify the program.

The basic contract for asserting the contract is

assertion. As a result, insert a checkable expression in the code, this expression must be true under normal circumstances:

ASSERT (Expression);

C The programmer has a feeling of recognition. But with C is different, in the function

Assert will thrown

Assertexception, this exception can be captured and processed. If the code must handle other code to the misuse, if the code must be a failure, the capture contract violation is useful, and it is also a powerful tool for debugging.

The prior condition of contract prior art and contractual conditions of contract The prior condition is indicated by the prior art conditions of statement execution. The most typical usage may have the validity of the verification function parameters. The results of the contract after testing conditions. The most typical method of use, the number verification function returns to the legality and any side effects it. The syntax structure is:

in

{

... Contract prior art ...

}

OUT (RESULT)

{

... Contracts Posts ...

}

Body

{

... code ...

}

According to the definition, if the prior art condition is violated, the process body (body) will be subject to the wrong parameters. This will throw an inexception exception. If the contractual examination conditions are violated, it means that there is a bug in the process body, and an OUTEXCEPTION is abnormal.

The IN or OUT clause can be omitted. If the process body has an OUT clause, the variable Result will be declared and assigned to the return value of the function. For example, we implements a function of square root:

Long Square_root (long x)

in

{

Assert (x> = 0);

}

OUT (RESULT)

{

Assert (result * result) == x);

}

Body

{

Return math.sqrt (x);

}

Assessment in In and Out is called

contract. Among them, any D's statement or expression, but must guarantee that these statements have no side effects, and the code in the final release does not depend on these codes. These code will not be included when the program is constructed.

If the function returns void, that is, there is no result, there is naturally no result declaration in the OUT sentence. In this case, use:

Void func ()

OUT

{

...contract...

}

Body

{

...

}

In the OUT statement,

Result is initialized and set to the return value of the function.

The compiler can be designed to be referenced in IN {} for each in and inout parameters, and each OUT and INOUT parameters are referenced in OUT {}.

The in-OUT statement can also be used inside the function, for example, can be used to check the result of the loop:

in

{

Assert (j == 0);

}

OUT

{

askERT (j == 10);

}

Body

{

For (i = 0; i <10; i )

J ;

}

This feature is currently not implemented.

IN, OUT and inheritance If the derived class function rewrites a function in the parent class, then it only needs to meet a base function.

In contract. Rewriting function

Relaxed the contract.

Conversely, all OUT contracts must be met, so the rewrite function tightened the OUT contract.

The invariant category of the class invariant class is used to specify the characteristics of the class in the class (except when executing member functions). They are in

Introduction in the class. .

bibliography

Contract recommendation

Let Java support contract

转载请注明原文地址:https://www.9cbs.com/read-54529.html

New Post(0)