CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2008
    Posts
    48

    Returning multiple state fields and preserving const-ness

    Say I need to return two things from a Validate function: an error code (every time, can be 'ok') and an error message (only if not ok). Exceptions are not appropriate here. At present this is done by returning the int code, and having an errorMessage_ field inside the object, which is looked at by a parent object if the validation fails. Problem is, this prevents the Validate function from being const (which it ideally would be), unless we declare errorMessage_ to be mutable (which seems inappropriate).

    I can think of a few ways out of this:
    1) status quo
    2) pass in a non-const string reference into the validate function, which is populated if needed.
    3) Declare an int + string struct, and return that.
    4) Declare an ErrorState object (which we may find useful for holding other data), and return that.

    2 would be ugly, 3 is simple but would add the cost of a lot of empty strings being constructed, 4 might get a bit expensive for ok states being returned from many validate functions - have a singleton 'ok' which is returned unless there is an error? Null object pattern? I guess 3 and 4 are very similar in principle.

    Is there a cleverer way of doing this?

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Returning multiple state fields and preserving const-ness

    I like your option 4.

    However, I may also take a different approach (which is a very C-style approach) by only returning the error code. When the error string is needed, I can call a translation function to retrieve the error message associated with the error code.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  3. #3
    Join Date
    Feb 2008
    Posts
    48

    Re: Returning multiple state fields and preserving const-ness

    The translation isn't always possible because sometimes member data are needed to populate the error string - therefore the string has to be created when the error is found.

  4. #4
    Join Date
    Mar 2003
    Posts
    126

    Re: Returning multiple state fields and preserving const-ness

    How about returning the string as the error code then? Again this is a very C-style approach (any error code return values are), but you could return a pointer to an error string on fail, 0 otherwise.

    Just curious, why are exceptions not appropriate?
    Cheers,
    Zen

  5. #5
    Join Date
    Feb 2008
    Posts
    48

    Re: Returning multiple state fields and preserving const-ness

    The program parses messages and does stuff. The domain is very complex. There are a lot of ways of sending malformed messages, and a lot of ways of sending well-formed messages which are not valid due to some external state. In usage there are roughly as many messages with errors as there are without. Raising errors like this is by no means exceptional, it is expected and part of the normal operation of the system. I understood that exceptions should be saved for genuine exceptional cases - network connectivity, database error, out of memory etc. - rather than used in place of return values.

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Returning multiple state fields and preserving const-ness

    Quote Originally Posted by Liche
    unless we declare errorMessage_ to be mutable (which seems inappropriate).
    I don't see why. Just create a 'Get_Last_Error' const function that returns the string. And make the error string private.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #7
    Join Date
    Feb 2008
    Posts
    48

    Re: Returning multiple state fields and preserving const-ness

    Quote Originally Posted by JohnW@Wessex
    I don't see why. Just create a 'Get_Last_Error' const function that returns the string. And make the error string private.
    But how can the error string be set within a const function if it isn't mutable? Returning the error isn't the problem, setting it is.

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Returning multiple state fields and preserving const-ness

    I didn't see the problem in making it mutable.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Returning multiple state fields and preserving const-ness

    Somehow I don't like keeping that error message or related info with your class in a mutable member. Not because of mutability but because usually the details about an error is not a part of the class' functional specifications. Imagine exception class objects being thrown on various operations with a vector being stored in the vector itself. Not nice.

    So, if you don't really want to go by the route of exception, which you should. But considering you have a convincing reason, you can use std::pair<ErrorNum, ErroString> as the return type for the function. If there can be more return values, you can use a tuple - see boost tuple library.

  10. #10
    Join Date
    Feb 2008
    Posts
    48

    Re: Returning multiple state fields and preserving const-ness

    Thanks for your input. I've experimented with a Result object being passed-by-reference, which is working nicely - it also saves having to propagate errors up several layers:

    Code:
    Object::Validate(Result& _result)
    {
        otherObject.Validate(_result);
        return;
    }
    
    OtherObject::Validate(Result& _result)
    {
        if(stuff) 
            _result.Set(error_code, "error message");
    
        return;
    }
    which is a nice bonus. I'd probably have liked to use the Java-style new keyword to construct the Result where the error was raised, but this isn't desirable in C++ because it would cause the client to have to delete the Result* - given the number of uses this is asking for a memory leak. C++ can't return reference-to-locally-declared-object, and using copy constructors was unnecessarily inefficient, so this seemed the best way of approaching it.

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Returning multiple state fields and preserving const-ness

    Quote Originally Posted by Liche
    The translation isn't always possible because sometimes member data are needed to populate the error string - therefore the string has to be created when the error is found.
    Okay, then how about embedding the code in the string, and adding a getCode function to extract it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured