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 93811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:24:11+00:00 2026-05-10T23:24:11+00:00

I’m interested in hearing what technique(s) you’re using to validate the internal state of

  • 0

I’m interested in hearing what technique(s) you’re using to validate the internal state of an object during an operation that, from it’s own point of view, only can fail because of bad internal state or invariant breach.

My primary focus is on C++, since in C# the official and prevalent way is to throw an exception, and in C++ there’s not just one single way to do this (ok, not really in C# either, I know that).

Note that I’m not talking about function parameter validation, but more like class invariant integrity checks.

For instance, let’s say we want a Printer object to Queue a print job asynchronously. To the user of Printer, that operation can only succeed, because an asynchronous queue result with arrive at another time. So, there’s no relevant error code to convey to the caller.

But to the Printer object, this operation can fail if the internal state is bad, i.e., the class invariant is broken, which basically means: a bug. This condition is not necessarily of any interest to the user of the Printer object.

Personally, I tend to mix three styles of internal state validation and I can’t really decide which one’s the best, if any, only which one is absolutely the worst. I’d like to hear your views on these and also that you share any of your own experiences and thoughts on this matter.

The first style I use – better fail in a controllable way than corrupt data:

void Printer::Queue(const PrintJob& job) {     // Validate the state in both release and debug builds.     // Never proceed with the queuing in a bad state.     if(!IsValidState())     {         throw InvalidOperationException();     }      // Continue with queuing, parameter checking, etc.     // Internal state is guaranteed to be good. } 

The second style I use – better crash uncontrollable than corrupt data:

void Printer::Queue(const PrintJob& job) {     // Validate the state in debug builds only.     // Break into the debugger in debug builds.     // Always proceed with the queuing, also in a bad state.     DebugAssert(IsValidState());      // Continue with queuing, parameter checking, etc.     // Generally, behavior is now undefined, because of bad internal state.     // But, specifically, this often means an access violation when     // a NULL pointer is dereferenced, or something similar, and that crash will     // generate a dump file that can be used to find the error cause during     // testing before shipping the product. } 

The third style I use – better silently and defensively bail out than corrupt data:

void Printer::Queue(const PrintJob& job) {     // Validate the state in both release and debug builds.     // Break into the debugger in debug builds.     // Never proceed with the queuing in a bad state.     // This object will likely never again succeed in queuing anything.     if(!IsValidState())     {         DebugBreak();         return;     }      // Continue with defenestration.     // Internal state is guaranteed to be good. } 

My comments to the styles:

  1. I think I prefer the second style, where the failure isn’t hidden, provided that an access violation actually causes a crash.
  2. If it’s not a NULL pointer involved in the invariant, then I tend to lean towards the first style.
  3. I really dislike the third style, since it will hide lots of bugs, but I know people that prefers it in production code, because it creates the illusion of a robust software that doesn’t crash (features will just stop to function, as in the queuing on the broken Printer object).

Do you prefer any of these or do you have other ways of achieving this?

  • 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. 2026-05-10T23:24:11+00:00Added an answer on May 10, 2026 at 11:24 pm

    The question is best considered in combination with how you test your software.

    It’s important that hitting a broken invariant during testing is filed as a high severity bug, just as a crash would be. Builds for testing during development can be made to stop dead and output diagnostics.

    It can be appropriate to add defensive code, rather like your style 3: your DebugBreak would dump diagnostics in test builds, but just be a break point for developers. This makes less likely the situation where a developer is prevented from working by a bug in unrelated code.

    Sadly, I’ve often seen it done the other way round, where developers get all the inconvenience, but test builds sail through broken invariants. Lots of strange behaviour bugs get filed, where in fact a single bug is the cause.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from

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.