[Go] Code contract

Source: Internet
Author: User

Code contracts contain classes for tagging code, static analyzers for compile-time parsing, and run-time analyzers. The classes of code contracts can be found in the System.Diagnostics.Contracts namespace.

The benefits of code contracts include:

  • Improved testing: Code contracts provide static contract validation, run-time checks, and document generation.

  • Automated testing Tools: You can use code contracts to filter out meaningless test parameters that do not meet the preconditions, resulting in more meaningful unit testing.

  • static validation: The static checker can determine if there are any violations of the contract without running the program.  

  • Reference Document: Document Builder adds contract information to an existing XML document file. A style sheet that can be used with Sandcastle is also provided so that the resulting document page has a contract section.

Contracts are immediately available to all. NET Framework languages, and you do not have to write special parsers or compilers. with Visual Studio add-ins, you can specify the level of code contract analysis to perform.   by authoring contracts in Visual Studio, you can take advantage of the standard IntelliSense provided by the tool.

#define directive. " The majority of the methods in the contract class are compiled under certain conditions, that is, the compiler emits calls to these methods only when you define special symbols contracts full using     #ifdef directives; can produce D Ifferent builds, some with contracts, and some without. " You can use contracts full to write contracts through code without having to use the   #ifdef   directives; You can produce different builds, some of them contain contracts, and some don't contain contracts.

For detailed instructions and tools for using code contracts, see E Code contracts on the MSDN devlabs Web site.

Preconditions

can usecontract. Requires method to represent preconditions. Preconditions specify the state when the method is called. They are typically used to specify valid parameter values.   All members mentioned in the preconditions must have at least the same accessibility as the method itself; otherwise, the preconditions may not be understood by all callers of the method.   The run-time behavior of the failed preconditions is determined by the run-time parser.

For example, the following preconditions indicate that the parameter x must be non-null.

Contract.Requires (x! = null);

If your code must throw a specific exception when the precondition fails, you can use the generic overload of Requires, as shown below.

Contract.requires<argumentnullexception> (x! = null, "X");

Legacy Requires Statements

if-then-throw code." > Most code is &NBSP, if- then- throw The   code form contains some parameter validation.   In the following cases, the Contract tool recognizes these statements as preconditions:

    • These statements appear before any other statements in the method.

    • The entire set of such statements is followed by an explicit contract method call, such as a call to a Requires, ensures,ensuresonthrow, or Endcontractblock method.

Whenwhen the if-then-Throw statement appears in this form, the tool recognizes these statements as old requires statements. if the if-then-throw sequence is not followed by any other contract, the code is contract. the end of the Endcontractblock method.

if (x = = null) throw new ... Contract.endcontractblock (); All previous "If" checks is preconditions

Please note that the condition in the above test is a negative predecessor condition. (The actual preconditions should beX! = null. A negative precondition is strictly limited: it must be written as shown in the example above, i.e. it should not contain   else  clause, and   then  clause body must be a single   throw  statement.  if test is subject to both purity and visibility rules (see usage Guidelines ), but the throw expression was subject only to purity rules. " > if  test is limited by the purity and visibility rules (see usage Guidelines), and   throw  Expressions are limited only by pure rules.  

postconditions

A post condition is a contract for the state at the time the method terminates. Check the post condition before exiting the method. The run-time behavior of a failed post condition is determined by the run-time parser.

Unlike preconditions, a post condition can refer to a member with a lower visibility. clients may not understand or take advantage of some of the information represented by a private state using a post condition, but this does not affect the ability of the client to use the method correctly.

Standard post conditions

You can use the ensures method to represent a standard post condition. a post condition represents a condition that must be true when the method terminates normally .

Contract.ensures (this. F > 0);

Exception post Condition

An exception post condition is a post condition that should be true when a method throws a particular exception . You can use contract. The Ensuresonthrow method is used to specify these post conditions, as shown in the following example.

Contract.ensuresonthrow<t> (this. F > 0);

This parameter is the condition that must be true whenever an exception is thrown as a subtype of T .

Some exception types are difficult to use in an exception post condition.For example, usingthe type of T Exception requires that the method guarantee the condition regardless of the type of exception thrown, even if the exception type is a stack overflow or other uncontrollable exception. You should only use exception post conditions for specific exceptions that may be thrown when a member is called, for example, when the TimeZoneInfo method is called . Invalidtimezoneexception is raised.

[Go] Code contract

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.