DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Code Contracts – Invariants

Denzel D. user avatar by
Denzel D.
·
Jul. 15, 10 · Interview
Like (0)
Save
Tweet
Share
4.22K Views

Join the DZone community and get the full member experience.

Join For Free

Compared to pre-conditions and post-conditions, invariants might seem like a quite different concept. Unlike the above mentioned contract types, invariants do not apply to methods but are used to verify the state of a class instance.

A class can contain multiple fields and properties that carry various information regarding the instance state and behavior. Invariants are used to make sure that every instance of the class that implements a contract is following the “directives” set by the contract, therefore satisfying the contract.

For example, there is the Car class:

class Car
{
public bool isWorking;
}

Obviously this is a very simple class and in practice you will work with way more complicated stuff, but to illustrate the use of invariants, it is perfect. The isWorking value represents whether the car (represented by the Car instance) is working or not.

Now, I am also creating a method that will change the state of the instance at random:

public void ChangeState()
{
Random random = new Random();
double result = random.NextDouble();
isWorking = Convert.ToBoolean(Math.Round(result));
}

I am using this to demonstrate that the contract will either fail or succeed depending on the state of isWorking. But so far – there is no contract.

For invariants, a single contract method should be introduced inside the class that will check the state of an instance. This method should be decorated with the ContractInvariantMethod attribute. Although not required, I highly recommend having only one such method inside the class.

No exception will be thrown if you use several methods with set to check the state, but this later on leads to performance issues – it is tied to how the process is taking place. Take a look at the diagram:

Once the object instance is initialized, every method set as ContractInvariantMethod inside the class is automatically triggered to check the state of the object. For every method included in the object, that is executed, the contract methods are once again triggered to check the state of the object. You can imagine how it affects the performance if several state checkers are being run once a single method is called, especially if there are multiple conditions and complex methods.

Now to the actual invariant method implementation. My method looks like this:

[ContractInvariantMethod]
void CheckState()
{
Contract.Invariant(isWorking);
}

Simple enough, it is using Contract.Invariant to check against a Boolean value. It can be a stament that results in a Boolean value, but in my case I am just checking an existing field. The method should be void and not contain any other code than Invariant calls.

NOTE: There is an overload for Invariant that allows you to set a message once the contract fails. All you have to do is add a second parameter that is a string.

Invariant methods are implicitly called at runtime, therefore you don’t have to write any additional calls. In case it is called, an error will show up:

NOTE: Invariant methods are not triggered if a method that implements System.IDisposable.Dispose (member of the class) is triggered.

Once the class is set, I am initializing an instance of it and call the ChangeState method.

Car car = new Car();
car.ChangeState();

I am not sure how the state value will be, since it is random, so running the program a couple of times will eventually result in satisfied and failed contracts.

Invariant (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Chat GPT-3 Changed the Life of Young DevOps Engineers
  • Kubernetes-Native Development With Quarkus and Eclipse JKube
  • Java REST API Frameworks
  • Apache Kafka Is NOT Real Real-Time Data Streaming!

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: