Skip to content

Instantly share code, notes, and snippets.

@cathode
Created April 12, 2013 06:26
Show Gist options
  • Select an option

  • Save cathode/5369905 to your computer and use it in GitHub Desktop.

Select an option

Save cathode/5369905 to your computer and use it in GitHub Desktop.
comparison of contracts vs traditional input checking.
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