How to use methods contracts and why?

Asked

Viewed 159 times

5

Reading a book on development . NET, I saw a brief description about methods Contracts and tried to know a little more on the internet.

It uses an imperative syntax that is costly and has low tool support. To use the contract in the library and in the application, it is necessary to perform a post-compilation process. Overall, it is an interesting project, but requires a first class compiler and support syntax to be useful.

I found something related to the following code:

public int Insert(T item, int index)
    requires index >= 0 && index <= Count
    ensures return >= 0 && return < Count
{ … }

Already on the Internet I also saw that has little content about it, and I would like to know what it is, and if it’s really worth using?

1 answer

5


There is no such syntax. There is a proposal for it to enter C#, and I hope very much to enter.

Today there is a contract library which does something similar, but is quite inferior. The use of contracts has existed since the . NET 4.0.

The use of it along with static analysis tools can be powerful, and indeed helped well. I don’t know why but the newer versions of Visual Studio no longer have the tools and the mechanism has become less useful, unless you do a lot of manual things or on your own.

Doesn’t mean he hasn’t used it yet, but he’s not much different than a if, at most gets more semantic. Anyway to be a first class mechanism need to be in the language and the compiler collaborate.

Contracts can be more interesting than typical mechanisms because they can be resolved at compile time and may even be deleted from the code becoming at no cost of execution time. Implemented in the right way can become part of the contract type and method, that is, whoever calls it must respect the contract directly. And in sophisticated mechanisms this can propagate and until only the input of the data is verified if it is within the contract eliminating all other checks because the data is already valid (is ideal, it is not simple to make it work in the language).

I like its use, but I admit that the current state of it on . NET is not so advantageous as it is, but also not so bad to avoid it. At least it gives more semantics and you can easily change later if the mechanism is improved.

I’m still trying to figure out why they are no longer emphasized. But the . NET code uses a lot, I think everyone should use more. Help give more robustness and readability.

Has wikipedia article on general use.

I answered something about them on Is there any functionality similar to Assert (affirmations) in C#?.

  • In theory then, should it ever be implemented, it would help in performance above just replacing some if within the method beyond readability?

  • 1

    In some cases, yes. Today you can do this with external tools, it can rewrite the code, but these tools have not evolved anymore and are no longer in VS.

  • All right! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.