Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6141337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:14:15+00:00 2026-05-23T18:14:15+00:00

Quite simply, when designing a new class, how do I figure out what the

  • 0

Quite simply, when designing a new class, how do I figure out what the invariant should be? What defines the invariant? I’ve heard that it’s tied into validity, but that’s still ambiguous. What makes a given instance valid or invalid is debatable.

Should I just go with my “gut” feeling? Are there guidelines for figuring out what the invariant is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:14:15+00:00Added an answer on May 23, 2026 at 6:14 pm

    Invariant can be always represented as predicate with arguments being some or all state variables (fields) of the class. One class can have more then one invariant. For example, suppose you have an Account class that has initialBalance, listOfTransactions, currentBalance. Also, we keep the transactions in a sorted (by date) list . for This class there are at least two invariants that should be maintained:

    1) initialBalance + sum(transaction amounts) = currentBalance
    2) for every element in the listOfTransactions, the timestamp of the transactions at position i should be always less then the timestamp of the transaction at position j if i < j.

    Invariants depend on what the class is doing and also how the class is implemented.

    Let say, we can add one more state variable: closedDate, and one more invariant will appear: no transaction can have date after the closeDate.

    Or if the list is not sorted by date, but by transaction amount, then the invatians would change.

    Another example:
    Let’s suppose that you have mutable Ellipse class defined with two fields, r1 and r2 which has setters for r1 and r2. This class doesn’t have any invariant, as any values for r1 and r2 can represent well defined ellipse.

    Now let’s suppose that you create new Circle class that extends the mutable Ellipse. The circle has only one radius add the invariant would be (r1==r2). In order to maintain the invariant, you have to disallow somebody setting r1 or r2, such that r1!=r2 happens.

    On the other hand if the Ellipse and Circle are immutable you don’t have to care for the invariants during the life of the objects, as the condition would be checked only during construction.

    With the previous examples, i wanted to explain that
    1) the way the invariants are established and maintained depends very much on the choice of design of the relations between the classes.
    2) What the class is doing
    3) How the class is implemented.

    Immutable classes tend to be less complex to maintain their invariant, as they are established at construction, and never change. (Immutability has many other benefits too – not in scope of the answer)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to that functional programming paradigm, but so far I like it.
I have a SQL query that is quite simply select * from tblOrders where
Quite simply I want to be able to test that a Asp.Net web forms
Quite simply this is my question: class A(models.Model): x = DateTimeField(...) class B(models.Model): x
Quite simply: is there a place such as DeviantArt, but purely for the purpose
This is quite frustrating. I am simply trying to create a dynamic text and
My question is quite simple and with the SharpSvn Api, it should be easy
I'm currently designing a custom tab control class which derives from System.Windows.Forms.Control. The problem
We're designing data import from an external source like MAS200 into our production SQL
Good day! I'm quite new to scala, so during development the following question was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.