Created
April 12, 2013 06:26
-
-
Save cathode/5369905 to your computer and use it in GitHub Desktop.
comparison of contracts vs traditional input checking.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void CheckUsage(object owner, string operationId) | |
| { | |
| // New code | |
| Contract.Requires<InvalidOperationException>(owner == this.owner, "End was called on a different object than Begin"); | |
| Contract.Requires<InvalidOperationException>(null != this.operationId, "End was called multiple times for this operation."); | |
| Contract.Requires<InvalidOperationException>(operationId == this.operationId, "End operation type was different than Begin."); | |
| // Old code | |
| if (!object.ReferenceEquals(owner, this.owner)) | |
| throw new InvalidOperationException("End was called on a different object than Begin"); | |
| else if (object.ReferenceEquals(null, this.operationId)) | |
| throw new InvalidOperationException("End was called multiple times for this operation."); | |
| else if (!string.Equals(operationId, this.operationId)) | |
| throw new InvalidOperationException("End operation type was different than Begin."); | |
| // cleanup | |
| this.operationId = null; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment