473,545 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class design question.

Is this class evidence of poor design?

class Rectangle
{
double width, height;
public:
double get_width() const { return width; }
double get_height() const { return height; }

void set_width(doubl e width) { this->width = width; }
void set_height(doub le height) { this->height = height; }
};

If so then why?
Jul 22 '05 #1
18 1411
* Jason Heyes:
Is this class evidence of poor design?

class Rectangle
{
double width, height;
public:
double get_width() const { return width; }
double get_height() const { return height; }

void set_width(doubl e width) { this->width = width; }
void set_height(doub le height) { this->height = height; }
};
Yes.

If so then why?


No user defined constructor => undefined state after construction.

And as it stands (it would be different with a constructor) no way
to assign both width and height at the same time.

Also there is evidence of poor coding, namely inconsistent use of
'this->'; even using it at all in C++ is generally poor style, but
using it inconsistently, just as a disambiguator, is worse.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41******** **************@ news.optusnet.c om.au...
Is this class evidence of poor design?

class Rectangle
{
double width, height;
public:
double get_width() const { return width; }
double get_height() const { return height; }

void set_width(doubl e width) { this->width = width; }
void set_height(doub le height) { this->height = height; }
};

If so then why?


If there are no class invariants to enforce, and no reason
to expect having to add this capability in the future, I
would consider using a plain struct instead:
struct Rectangle {
//consider adding a constructor...
double width, height;
};
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #3
"Jason Heyes" writes:
Is this class evidence of poor design?

class Rectangle
{
double width, height;
public:
double get_width() const { return width; }
double get_height() const { return height; }

void set_width(doubl e width) { this->width = width; }
void set_height(doub le height) { this->height = height; }
};

If so then why?


Yes, it's poor design. It is just an obfuscated way of doing what could be
done with a simple struct. There should be some reward for complexity,
there are no rewards here.
Jul 22 '05 #4
Jason Heyes wrote:
Is this class evidence of poor design?

class Rectangle
{
double width, height;
public:
double get_width() const { return width; }
double get_height() const { return height; }

void set_width(doubl e width) { this->width = width; }
void set_height(doub le height) { this->height = height; }
};

If so then why?


Poor design in terms of what? Notation? Mechanics?

Some might argue that since you are giving read -and- write access to width
and height, you could as well make width and height public (like someone
else already mentioned).

However, I think in terms of OOP your approach is better, because you are
e.g. free to change the names of your class members at any time without
affecting the actual use your class for a potential client.

As to the style thing, I'd prefer notating data members with m_* rather than
using 'this' to express membership, but that's just a matter of taste I
guess.
Jul 22 '05 #5
osmium wrote:
Yes, it's poor design. It is just an obfuscated way of doing what could
be
done with a simple struct. There should be some reward for complexity,
there are no rewards here.


If the class will never change, then you might be right.

Otherwise this is very short thought.
If for example some day you happen to want the class doing more tha just
setting the width or height, like e.g. logging the action, then in your
approach you would have to completely redesign your class. The client would
have to change ALL his calls where he is settings width and height.

That can't happen with his approach, thus, in my eyes, it's by far better
style than making it a struct (which I try to avoid in general in C++,
since it's more a backward compatibility to C thing instead of a useful C++
construct).
Jul 22 '05 #6
"Matthias Käppler" writes:
osmium wrote:
Yes, it's poor design. It is just an obfuscated way of doing what could
be
done with a simple struct. There should be some reward for complexity,
there are no rewards here.


If the class will never change, then you might be right.

Otherwise this is very short thought.
If for example some day you happen to want the class doing more tha just
setting the width or height, like e.g. logging the action, then in your
approach you would have to completely redesign your class. The client
would
have to change ALL his calls where he is settings width and height.

That can't happen with his approach, thus, in my eyes, it's by far better
style than making it a struct (which I try to avoid in general in C++,
since it's more a backward compatibility to C thing instead of a useful
C++
construct).


I think the notion of writing for the future is mostly bullshit. The idea
that people can realistically predict the future is bogus. This is an
example of OOP gone wild. One of the first steps in design is to decide
what you want to design. An airplane that characteristica lly does a
controlled crash landing on an aircraft carrier should have a different
design than an
airplane that lands on a runway. A composite design penalizes both
applications.

There is entirely too much hokum involving OOP, it is not a silver bullet
and the sooner people realize that the better.

Jul 22 '05 #7
osmium wrote:
I think the notion of writing for the future is mostly bullshit. The idea
that people can realistically predict the future is bogus.
Exactly, that's what I'm talking about. And that's why it's often a good
idea to design your class loose so you can extend it at any time without
changing its outer appearance.
This is an
example of OOP gone wild. One of the first steps in design is to decide
what you want to design.
If your designs hold 100% from day one to release, than you're an extremely
good software engineer :)
Most designs do not, that's why it's a good idea to keep some doors open.
There is entirely too much hokum involving OOP, it is not a silver bullet
and the sooner people realize that the better.


Opinions may differ here I guess.

Cheers,
Matthias
Jul 22 '05 #8
"osmium" <r1********@com cast.net> wrote in message
news:30******** *****@uni-berlin.de...
"Matthias Käppler" writes:
osmium wrote: I think the notion of writing for the future is mostly bullshit. The idea
that people can realistically predict the future is bogus. This is an
example of OOP gone wild. One of the first steps in design is to decide
what you want to design. An airplane that characteristica lly does a
controlled crash landing on an aircraft carrier should have a different
design than an
airplane that lands on a runway. A composite design penalizes both
applications.


But designing to allow for ease of modification is the point, not knowing
the future.

There is entirely too much hokum involving OOP, it is not a silver bullet
and the sooner people realize that the better.


Amen to that, brother.
--
Gary
Jul 22 '05 #9
osmium wrote:
....

I think the notion of writing for the future is mostly bullshit. The idea
that people can realistically predict the future is bogus. This is an
example of OOP gone wild. One of the first steps in design is to decide
what you want to design. An airplane that characteristica lly does a
controlled crash landing on an aircraft carrier should have a different
design than an
airplane that lands on a runway. A composite design penalizes both
applications.

There is entirely too much hokum involving OOP, it is not a silver bullet
and the sooner people realize that the better.


There is a balance between overdoing it and underdoing it.

The way I express it is:
"Never artificially limit the utility of a class or function"

In other words, don't go out of your way to make somthing less generic.

As for the OP. I have no idea what the application of the class is so
it might be just perfect, it might not, I don't know.

If I needed a generic shape library, it's not even close.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
6297
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the...
13
2356
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating...
15
9038
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To...
3
1804
by: fernandez.dan | last post by:
I'm still learning how to use Object Oriented concepts. I'm have a basic question dealing with design. I have two classes that deal with I/O pertaining to network and usb that inherit from an abstract parent with four basic functions: Open, Read, Write, and Close. // Note alot of the stuff has been left out // just to give a basic picture...
1
2258
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual ~A() ;
11
2219
by: dimension | last post by:
If my intentions are to create objects that encapsulates data rows in a table, is it better to use a Struct or Class? Based on what i read, if my objects will simply have get and set methods, Struct is may be better...but i am looking for some advise from the experts on this? Assumptions: it is possible for some operations, i may have 8000 to...
6
2107
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
43
2042
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions within main? (It just doesn't "feel" right to me). Example 1: class MyProg { public:
6
2124
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often times when I program, I will create objects for a specific purpose for a program and if I need to add to it I just add the code.
29
2206
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a "new" contract. I have a method in my contract class called "Save" which is called like this... dim oContract as new Contract
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.