473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Public Data in Private Class or Private Data in Public Class?

In a book on Data Structures that I'm reading, the authors are
describing various linked lists and trees. In these, they start with
some form of node class. What's driving me crazy is that they define
these such that the data in those node classes is public. As a
specific example (a node they're going to use to build linked lists):

template <typename T>
class node
{
public:
T nodeValue; // data held by the node
node<T> *next; // next node in the list

node() : next(NULL) // default constructor w no init value
{}

node (const T& item, node<T> *nextNode = NULL) :
nodeValue(item), next(nextNode)
{}
};

They then use this node class (with its public data) in the private
areas of their linked list classes. They justify this by saying:
"Making the data in the node objects public does not violate the
object-design principle of information hiding, because end users do not
have access to these low-level objects."

In my ignorance, my feeling is that this is bunk. THEY have access to
those public data items and can mess things up themselves. Plus, if
they change the implementation of node, they'll have to change the code
wherever they use it (standard software engineering stuff). Wouldn't
it be better to make the data (nodeValue and *next) of that node class
private and then provide public setNode and getNode methods? I suppose
there might be some additional overhead in making the setNode and
getNode calls vs just changing the data directly. But, without
measuring the choke points in the completed application, there's no
telling if it makes a difference.

I'm really hoping I'm wrong about this, since it really frosts my
shorts that a couple of Computer Science professors wrote a book with
this kind of programming throughout it.

Jul 23 '05 #1
3 1526

"DaveLessnau" <dl******@mail.com> writes:
In a book on Data Structures that I'm reading, the authors are
describing various linked lists and trees. In these, they start with
some form of node class. What's driving me crazy is that they define
these such that the data in those node classes is public. As a
specific example (a node they're going to use to build linked lists):

template <typename T>
class node
{
public:
T nodeValue; // data held by the node
node<T> *next; // next node in the list

node() : next(NULL) // default constructor w no init value
{}

node (const T& item, node<T> *nextNode = NULL) :
nodeValue(item), next(nextNode)
{}
};

They then use this node class (with its public data) in the private
areas of their linked list classes. They justify this by saying:
"Making the data in the node objects public does not violate the
object-design principle of information hiding, because end users do not
have access to these low-level objects."

In my ignorance, my feeling is that this is bunk. THEY have access to
those public data items and can mess things up themselves. Plus, if
they change the implementation of node, they'll have to change the code
wherever they use it (standard software engineering stuff). Wouldn't
it be better to make the data (nodeValue and *next) of that node class
private and then provide public setNode and getNode methods? I suppose
there might be some additional overhead in making the setNode and
getNode calls vs just changing the data directly. But, without
measuring the choke points in the completed application, there's no
telling if it makes a difference.

I'm really hoping I'm wrong about this, since it really frosts my
shorts that a couple of Computer Science professors wrote a book with
this kind of programming throughout it.


Just goes to show that it was an egregious error even to allow
public data members in the first place. Hopefully, future generations
will learn from our tragic errors.

-SEan

Jul 23 '05 #2
DaveLessnau wrote:
In a book on Data Structures that I'm reading, the authors are
describing various linked lists and trees. In these, they start with
some form of node class. What's driving me crazy is that they define
these such that the data in those node classes is public. As a
specific example (a node they're going to use to build linked lists):
[snip example code]
They then use this node class (with its public data) in the private
areas of their linked list classes. They justify this by saying:
"Making the data in the node objects public does not violate the
object-design principle of information hiding, because end users do
not have access to these low-level objects."
I agree with that justification.
In my ignorance, my feeling is that this is bunk. THEY have access
to those public data items and can mess things up themselves.
That private class is just that, private. You don't use public
accessor functions to modify private data within the class
implementation. If you can't trust yourself to do it right, whom can
you trust?
Plus, if they change the implementation of node, they'll have to
change the code wherever they use it (standard software engineering
stuff). Wouldn't it be better to make the data (nodeValue and *next)
of that node class private and then provide public setNode and
getNode methods? I suppose there might be some additional overhead
in making the setNode and getNode calls vs just changing the data
directly. But, without measuring the choke points in the completed
application, there's no telling if it makes a difference.
One of the fundamental ideas of C++ is that you don't pay for what you
don't use. Such a library would force the function call overhead on
its users. From the users' point of view, it goes like this: "Why
should we pay for the overhead of a function call because the author
didn't trust himself to write the code correctly?" As a library
writer, you can't know in advance what effects your code will have on
performance. Therefore, you should try to make it as tight as
possible.
I'm really hoping I'm wrong about this, since it really frosts my
shorts that a couple of Computer Science professors wrote a book with
this kind of programming throughout it.


While debugging, the function calls you suggest can be useful. You
could include assertions to make sure you aren't violating any class
invariants. However, I think these should be removed for production
code.

Kristo

Jul 23 '05 #3

"DaveLessnau" <dl******@mail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
In a book on Data Structures that I'm reading, the authors are
describing various linked lists and trees. In these, they start with
some form of node class. What's driving me crazy is that they define
these such that the data in those node classes is public. As a
specific example (a node they're going to use to build linked lists):

template <typename T>
class node
{
public:
T nodeValue; // data held by the node
node<T> *next; // next node in the list

node() : next(NULL) // default constructor w no init value
{}

node (const T& item, node<T> *nextNode = NULL) :
nodeValue(item), next(nextNode)
{}
};

They then use this node class (with its public data) in the private
areas of their linked list classes. They justify this by saying:
"Making the data in the node objects public does not violate the
object-design principle of information hiding, because end users do not
have access to these low-level objects."

In my ignorance, my feeling is that this is bunk. THEY have access to
those public data items and can mess things up themselves. Plus, if
they change the implementation of node, they'll have to change the code
wherever they use it (standard software engineering stuff). Wouldn't
it be better to make the data (nodeValue and *next) of that node class
private and then provide public setNode and getNode methods? I suppose
there might be some additional overhead in making the setNode and
getNode calls vs just changing the data directly. But, without
measuring the choke points in the completed application, there's no
telling if it makes a difference.

I'm really hoping I'm wrong about this, since it really frosts my
shorts that a couple of Computer Science professors wrote a book with
this kind of programming throughout it.


Without seeing the class that uses this, I can't comment on whether there is
any way for end-users of the containing class to get at the private data.
But assuming it's private as stated, and not otherwise exposed (via a
GetNode call, for example), then there's no problem regarding information
hiding here. (And no mechanism can prevent you from messing up your own
class' internals, private or otherwise, so that part of your comments is
irrelevant.)
Also, in this example at least, the object is so simple that I don't see
much problem in simply exposing the internals as public. If the object were
more complicated, then I'd agree that its internals should be handled via
function calls. But in this case it's simply a data value and a next
pointer, which isn't as likely to change. If you were to want to add a
previous pointer to the node, for example, then you'd likely be reworking
the linked list code anyway, to make it doubly-linked.

So, in my opinion anyway, this isn't a "bad" thing, at least not in this
case. Show me a case where they're using a more complicated object in this
manner, something with some business logic behind it for example, and I'll
agree with you that it's not such a good idea. But here, it's just too
simple to bother.

-Howard



Jul 23 '05 #4

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

Similar topics

10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
3
by: Joe Fromm | last post by:
Perhaps I'm missing something obvious, but I've been curious about one of the coding practices I see advocated. I'm a longtime C/C++ programmer trying to learn C#, and I started looking around for...
4
by: Peter Speybrouck | last post by:
I have a little problem with a webservice. I reproduced the problem in the following simplified example. I just create a new C# ASP.NET webservice and a c# console application. I added a new...
3
by: Gladys | last post by:
Dear newsgroup - do not reply to this e-mail address. I have just created it to avoid getting spam to my real address. Reply to the newsgroup. I am watching them all now so please reply now. ...
27
by: thomasp | last post by:
Variables that I would like to make available to all forms and modules in my program, where should I declare them? At the momment I just created a module and have them all declared public there. ...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
17
by: iesvs | last post by:
Hello, i want to create c++ libraries (for my use) but I don't know how I must do to have headers which contain only the public declarations. I used in C to put the private declarations in *.c...
11
by: TinaJones095 | last post by:
Hello I am going to give a program that I have done, but I have to modifiy it, but I need help okay can you help ? Here the program I need help to straighten up below: the Java error is right at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.