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

  • SEARCH
  • Home
  • 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 6031361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:10:22+00:00 2026-05-23T05:10:22+00:00

In the design by contracts, the class invariant must be satisfied on two occasions:

  • 0

In the design by contracts, the class invariant must be satisfied on two occasions: after creating the object and after call a routine. Are there any examples or conditions, which I have to do the evaluation before the call to the routine too?

  • 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-23T05:10:22+00:00Added an answer on May 23, 2026 at 5:10 am

    The class invariant can be violated before a feature call. The conditions may be different, I’m presenting only the most obvious ones:

    1. Aliasing. An object references some other object that is involved in a class invariant and that other object is modified by a third party:

      class SWITCH -- Creation procedure is ommitted for brevity.
      feature
          toggle1, toggle2: TOGGLE -- Mutually exclusive toggles.
          ...
      invariant
          toggle1.is_on = not toggle2.is_on
      end
      

      Now the following code violates the invariant of class SWITCH:

      switch.toggle1.turn_on -- Make `switch.toggle1.is_on = True`
      switch.toggle2.turn_on -- Make `switch.toggle2.is_on = True`
      switch.operate -- Class invariant is violated before this call
      
    2. External state. An object is coupled with external data that is referenced in a class invariant and may change unexpectedly:

      class TABLE_CELL feature
          item: DATA
              do
                  Result := cache -- Attempt to use cached value.
                  if not attached Result then
                          -- Load data from the database (slow).
                      Result := database.load_item (...)
                      cache := Result
                  end
              end
      feature {NONE} -- Storage
          cache: detachable DATA
      invariant
          consistent_cache: -- Cache contains up-to-date value.
              attached cache as value implies
              value ~ database.load_item (...)
      end
      

      Now if the database is modified outside the application, the cache may become inconsistent and a class invariant violation is triggered before the following feature call:

      data := table_cell.item -- Class invariant is violated before this call.
      
    3. Callback. An object can be passed to the other one in an invalid state:

      class HANDLER feature
          process (s: STRUCTURE)
              do
                  ... -- Some code that sets `is_valid` to False.
                  s.iterate_over_elements (Current)
              end
          process_element (e: ELEMENT)
              do
                  ...
              end
          is_valid: BOOLEAN
              do
                  ...
              end
      invariant
          is_valid
      end
      

      A callback to HADNLER, performed by the feature iterate_over_elements of the class STRUCTURE, causes an invariant violation because handler is not in a good condition:

      handler.process_element (...) -- Class invariant is violated before the call.
      

    One can argue that all the cases are because of software bugs and flaws, but this is exactly the purpose of class invariants to catch those including the cases when the violation happens before feature calls.

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

Sidebar

Related Questions

There are two way for choosing between abstract class or interface. Microsoft solution and
I'm familiar with object-oriented architecture, including use of design patterns and class diagrams for
In the book .NET Domain-Driven Design with C# from Tim McCarthy there's an example
I often design system where I have a class in my system that has
There is a Login function on a MessengerClient class. The MessengerClient class has a
WCF promotes good design by using interfaces and contracts etc. What baffles me is
I design new IntSet Class that use ArrayList. first, i extends Intset by ArrayList
Was: Design by contract: predict methods needed, discipline yourself and deal with code that
Design of SQL Table Users: Columns: ID(Primary Key), Name, Age, Zip. I am displaying
Duplicate : Design & Coding - top to bottom or bottom to top? I

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.