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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:40:47+00:00 2026-05-26T23:40:47+00:00

my Getter/Setter methods check the value, before they set/return it. When the value is

  • 0

my Getter/Setter methods check the value, before they set/return it. When the value is invalid, they throw an exception (BadArgumentException or IllegalStateException). This is needed since we initialize all members with invalid values – and so we avoid working with these invalid values ( == getting errors/segfaults/exception in other places).

The benefits are:

  • When you receive a member-value from a model, you know that they are valid
  • The validity checks are only performed in the model-object
  • The value range is defined in the model-object

This seems to be very unusual, since most new team-member first complain about it – even if they agree with me after I explain it to them.

Question: Is this a good programming style? (Even if it wasts a bit of performance)

Example Code:

inline bool MyClass::HasGroupID const { return m_iGroupID != 0; }

int MyClass::GetGroupID() const
{
    if( !HasGroupID() )
        throw EPTIllegalStateException( "Cannot access uninitialized group ID!" );

    return m_iGroupID;

}   // END GetGroupID() const

void MyClass::SetGroupID( int iGroupID )
{
    // negative IDs are allowed!
    if( iGroupID != 0 )
        throw EPTBadArgumentException( "GroupID must not be zero!", iGroupID );

    m_iGroupID = iGroupID;

}   // END SetGroupID( int )

Usage:

// in serialization
if( myObject.HasGroupID() )
    rStream.writeAttribute( "groupid", GetGroupID() );

// in de-serialization
try {
    // required attribute - throw exception if value is invalid
    myObject.SetGroupID( rStream.GetIntegerAttribute( "groupid" );
} catch( EPTBadArgumentException& rException )
{
    throw EPTBadXmlAttribute( fileName, rException );
}
  • 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-26T23:40:48+00:00Added an answer on May 26, 2026 at 11:40 pm

    I think it’s common style, and it’s certainly one of the core motivations for getter/setters in the first idea. However, in your particular example, I don’t think they make sense and I think that in general there are often better alternatives:

    If calling GetGroupID raises an exception if HasGroupID yields false, then this is a programmer error – so you should probably use an assert. In fact, why is it possible to have an object without a group ID in the first place? This sounds like a slightly inelegant design.

    Furthermore, if only non-zero IDs are valid, then you should try to enforce this restriction even earlier, using a dedicated type. That way, all the code dealing with group IDs can be sure that the IDs are valid (in fact, the compiler enforces this!) and you can’t possibly forget to check in some place. Consider:

    class GroupID {
    public:
        explicit GroupID( int id ) : m_id( id ) {
            if ( m_id == 0 ) {
                throw EPTBadArgumentException( "GroupID must not be zero!", m_id );
            }
        }
    
        // You may want to allow implicit conversion or rather use an explicit getter
        operator int() const { return m_id; }
    };
    

    Using this, you can just write

    void MyClass::SetGroupID( GroupID id )
    {
        // No error checking needed; we *know* it's valid, because it's a GroupID!
        // We can also use a less verbose variable name without losing readability
        // because the type name reveals the semantics.
        m_iGroupID = id;
    
    } // END SetGroupID( int )
    

    In fact, since group IDs are now a dedicated type, you could rename your function to just Set and use overloads. So you have MyClass::Set(UserID) and MyClass::Set(GroupID) and so on.

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

Sidebar

Related Questions

Normally to realized linked objects, i usually use getter and setter methods and this
I want to create getter/setter methods dyanmically to retrieve private properties. This is what
I know java and would normally put in getter/setter methods. I am interested in
I want Eclipse to automatically generate Javadoc comments for my getter and setter methods
How can you describe the parameters and return type (getter/setter) of your PHP functions?
I have an Objective-C NSMutableDictionary declared inside a class's @interface section, with getter/setter methods,
I have a model class with getter and setter methods, and the occasional static
We're often told we should protect encapsulation by making getter and setter methods (properties
I have seen member variables given a private modifier and then using getter/setter methods
I am trying to define simple getter/setter methods for a mixin class that 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.