Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contract.Requires vs Contract.Require

Tags:

.net

contract

I noticed that Microsoft named their code-contract-relative functions in .NET 4 in a strange manner.

They add "s" at the end of "require" and "ensure", so there are Contract.Requires() and Contract.Ensures(), but not at the end of "assert" and "assume", so there are Contract.Assert() and Contract.Assume(). The difference makes me a little confused.

In fact, my real problem is, I'm trying use code-contract in PHP, so I write something to imitate the "Contract" class in .NET 4. Since PHP has no build-in method to validate the type of parameters, I add a method to my own Contract class to do some validation. I choose the word "expect", because I think "expect parameter 'bar' to be string but..." is a common message when parameter type is wrong. And there comes the problem. Should I name my method Contract.Expect() or should I name it Contract.Expects()?

I'm from a non-english-speaking country, so sorry for my poor english. May be it's actually a english-language question, but I think only programmers can help me. So sorry for that if this question is not proper here.

like image 806
ani Avatar asked Apr 23 '11 07:04

ani


1 Answers

I believe Ensures and Requires are describing what methods need and/or guarantee, whereas Assert and Assume are commands to the contract verifier.

Or in other words, the former two describe preconditions/postconditions about the method's outside interface, whereas the latter two are just metadata to help the contract verifier do its job for you inside the method. One is relevant to the user, but the other one isn't.

like image 175
user541686 Avatar answered Oct 20 '22 00:10

user541686