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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:49:54+00:00 2026-05-15T03:49:54+00:00

I have a setup that looks like this. class Checker { // member data

  • 0

I have a setup that looks like this.

class Checker
{   // member data
    Results m_results; // see below
 public:
    bool Check();
 private:
    bool Check1();
    bool Check2();
    // .. so on
};

Checker is a class that performs lengthy check computations for engineering analysis. Each type of check has a resultant double that the checker stores. (see below)

bool Checker::Check()
{   // initilisations etc.
    Check1();
    Check2();
    // ... so on
}

A typical Check function would look like this:

bool Checker::Check1()
{   double result;
    // lots of code
    m_results.SetCheck1Result(result);
}

And the results class looks something like this:

class Results
{   double m_check1Result;
    double m_check2Result;
    // ...
public:
    void SetCheck1Result(double d);
    double GetOverallResult()
    { return max(m_check1Result, m_check2Result, ...); }
};

Note: all code is oversimplified.

The Checker and Result classes were initially written to perform all checks and return an overall double result. There is now a new requirement where I only need to know if any of the results exceeds 1. If it does, subsequent checks need not be carried out(it’s an optimisation). To achieve this, I could either:

  • Modify every CheckN function to keep check for result and return. The parent Check function would keep checking m_results. OR
  • In the Results::SetCheckNResults(), throw an exception if the value exceeds 1 and catch it at the end of Checker::Check().

The first is tedious, error prone and sub-optimal because every CheckN function further branches out into sub-checks etc.

The second is non-intrusive and quick. One disadvantage is I can think of is that the Checker code may not necessarily be exception-safe(although there is no other exception being thrown anywhere else). Is there anything else that’s obvious that I’m overlooking? What about the cost of throwing exceptions and stack unwinding?

Is there a better 3rd option?

  • 1 1 Answer
  • 1 View
  • 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-15T03:49:55+00:00Added an answer on May 15, 2026 at 3:49 am

    I don’t think this is a good idea. Exceptions should be limited to, well, exceptional situations. Yours is a question of normal control flow.

    It seems you could very well move all the redundant code dealing with the result out of the checks and into the calling function. The resulting code would be cleaner and probably much easier to understand than non-exceptional exceptions.
    Change your CheckX() functions to return the double they produce and leave dealing with the result to the caller. The caller can more easily do this in a way that doesn’t involve redundancy.
    If you want to be really fancy, put those functions into an array of function pointers and iterate over that. Then the code for dealing with the results would all be in a loop. Something like:

    bool Checker::Check()
    {
      for( std::size_t id=0; idx<sizeof(check_tbl)/sizeof(check_tbl[0]); ++idx ) {
        double result = check_tbl[idx]();
        if( result > 1 ) 
          return false; // or whichever way your logic is (an enum might be better)
      }
      return true;
    }
    

    Edit: I had overlooked that you need to call any of N SetCheckResultX() functions, too, which would be impossible to incorporate into my sample code. So either you can shoehorn this into an array, too, (change them to SetCheckResult(std::size_t idx, double result)) or you would have to have two function pointers in each table entry:

    struct check_tbl_entry {
      check_fnc_t checker;
      set_result_fnc_t setter;
    };
    
    check_tbl_entry check_tbl[] = { { &Checker::Check1, &Checker::SetCheck1Result }
                                  , { &Checker::Check2, &Checker::SetCheck2Result }
                                  // ...
                                  };
    
    bool Checker::Check()
    {
      for( std::size_t id=0; idx<sizeof(check_tbl)/sizeof(check_tbl[0]); ++idx ) {
        double result = check_tbl[idx].checker();
        check_tbl[idx].setter(result);
        if( result > 1 ) 
          return false; // or whichever way your logic is (an enum might be better)
      }
      return true;
    }
    

    (And, no, I’m not going to attempt to write down the correct syntax for a member function pointer’s type. I’ve always had to look this up and still never ot this right the first time… But I know it’s doable.)

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

Sidebar

Related Questions

Ok I have a page setup that looks like this: <div class=parent>Content here</div> I
I have a ListView setup in details mode that looks like this: When the
I have a standard list view setup with backbone that looks something like below.
I encountered a class during my work that looks like this: public class MyObject
I currently have a unittest.TestCase that looks like.. class test_appletrailer(unittest.TestCase): def setup(self): self.all_trailers =
I have a Transfer class, simplified it looks like this: public class Transfer {
I have an abstract class in a C++ program that looks like class Interface
I have an existing table structure that looks something like this: AnimalTable ------------- |Id
I currently have a div whose css looks like this: .class { overflow:auto; resize:
I have an app file that looks like this ws_app.rb: require 'rubygems' require 'sinatra'

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.