473,288 Members | 1,705 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,288 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 1522

"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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.