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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:31:50+00:00 2026-05-25T16:31:50+00:00

If I have a simple class such as… class Rectangle { private: double length;

  • 0

If I have a simple class such as…

class Rectangle
{
  private:
    double length;
    double width;
  public:
    void setLength(double l) { length = l; }
    void setWidth(double w) { width = l; }
    void getLength() { return length; }
    void getWidth() { return width; }
    void getArea() { return length * width; }
};

…and the person using it calls the mutator setWidth() with an invalid argument, say -1.0, what is the correct way to handle this? When I say correct, should I for example, change the return type from void to bool or possibly an int and return a value to indicate whether or not the operation occurred successfully, or am I to allow the value to be set since theoretically it will not break anything, but any values returned as a result, say from getArea() will be garbage and the user will have to debug what he/she did wrong?

Sorry if this is a silly question, but two text books later, and I’m still not quite sure about how classes I write should be implemented for others to use.

  • 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-25T16:31:51+00:00Added an answer on May 25, 2026 at 4:31 pm

    You have a couple choices. In the real world, it will depend on who is using your class (other internal code or public API) as well as conventions agreed upon by your company.

    You can:

    Throw Exception

    Pros:

    1. Simple to implement
    2. Keeps error handling code clean
    3. Allows passing of error messages and other variables
    4. Standard for most exceptional situations
    5. Harder to ignore than a boolean return
    6. Can be thrown from constructor

    Cons:

    1. Can be overused in areas where normal conditional statements should be used (this situation doesn’t count)
    2. You are introducing more “invisible” breaking points into an application. Many times users of a class don’t know what exactly is going to be thrown because it either isn’t documented or he/she didn’t read the documentation.

    Assert

    Pros:

    1. Rule of thumb: if you are going to crash, crash early
    2. Good for situations where you as the developer call the code and have control over the input
    3. Strong debugger support (typically)

    Cons:

    1. Gets excluded from non-debug builds so not good for times when user can still input bad data

    Boolean Returns

    Pros:

    1. Quick/Easy to implement
    2. Smaller footprint than exception handling (although this is negligible these days)

    Cons:

    1. Can’t pass back any extra information to the caller (why did it break?)
    2. Breaks down when you are trying to be consistent through your entire project and a simple boolean won’t cut it
    3. Easier to ignore compared to exceptions

    Evaluate Data Type

    Don’t be afraid to go back and check if the data type of your property makes sense for what you are doing. In this case, it does. However, I can think of times where developers have used standard integers when they really wanted unsigned integers or strings when they really needed a single character.

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

Sidebar

Related Questions

If I have a simple class such as:- @XmlRootElement public class MyClass { @XmlAttribute(required=true)
Say I have a simple object such as class Something { public int SomeInt
Let's say I have a regular simple Java class, such as: public class Foo
I have a simple object as such: public class Info { public string Name
Suppose I have simple class like: class MyClass { private $_prop; public function getProp()
I currently have a very simple class: public class Music { private Sequence sequence;
I have the simple class using auto-implemented properies: Public Class foo { public foo()
I have a simple class I'm serializing. [DataContract(Name = Test, Namespace = )] public
Assume I have such simple model: class Foo(models.Model): name = models.CharField(max_length=25) b_date = models.DateField()
I have a simple 'dimensions' class containing a width/height property and a constructor to

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.