473,378 Members | 1,110 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,378 software developers and data experts.

design question

This is a question related to my previous post titled "private member
functions:"

I recently saw that a colleage of mine submitted code which defines a
class Interface as follows[1]:

class Interface {
public:
Interface(UINT maxidx);

void set(UINT idx, UCHAR data);
/* ... */
private:
UINT d_maxidx;

bool isInRange(UINT idx) { return idx < d_maxidx; }
};

My first reaction to this code is that isInRange should have been
defined as a static function in the Interface.cc translation unit rather
than a member function:

static bool isInRange(UINT idx, UINT max) { return idx < max; }

Is my reaction correct? Any opinions?

/david

[1] simplified from original version

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #1
10 3293
David Rubin wrote:
I recently saw that a colleage of mine submitted code which defines a
class Interface as follows[1]:
[snip - class definition] My first reaction to this code is that isInRange should have been
defined as a static function in the Interface.cc translation unit rather
than a member function:


Forgot to mention that this class will not be used as a base class.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #2
"David Rubin" <bo***********@nomail.com> wrote...
This is a question related to my previous post titled "private member
functions:"

I recently saw that a colleage of mine submitted code which defines a
class Interface as follows[1]:

class Interface {
public:
Interface(UINT maxidx);

void set(UINT idx, UCHAR data);
/* ... */
private:
UINT d_maxidx;

bool isInRange(UINT idx) { return idx < d_maxidx; }
};

My first reaction to this code is that isInRange should have been
defined as a static function in the Interface.cc translation unit rather
than a member function:

static bool isInRange(UINT idx, UINT max) { return idx < max; }

Is my reaction correct? Any opinions?


There is no "correct" or "incorrect" reaction. It's your reaction,
nobody is going to tell you how to react.

As to the interface, my reaction to your reaction is "what the hell
is wrong with a non-static member?"

Victor
Jul 19 '05 #3
Victor Bazarov wrote:
class Interface {
public:
Interface(UINT maxidx);

void set(UINT idx, UCHAR data);
/* ... */
private:
UINT d_maxidx;

bool isInRange(UINT idx) { return idx < d_maxidx; }
};

My first reaction to this code is that isInRange should have been
defined as a static function in the Interface.cc translation unit rather
than a member function:

static bool isInRange(UINT idx, UINT max) { return idx < max; }

[snip] As to the interface, my reaction to your reaction is "what the hell
is wrong with a non-static member?"


It seems to pollute the interface. It's good to do error checking, but
why should this be an explicit part of the class definition? (Note my
self-reply that this class will only be used as a leaf.)

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #4
David Rubin wrote:
> class Interface {
> public:
> Interface(UINT maxidx);
>
> void set(UINT idx, UCHAR data);
> /* ... */
> private:
> UINT d_maxidx;
>
> bool isInRange(UINT idx) { return idx < d_maxidx; }
> };
>
> My first reaction to this code is that isInRange should have been
> defined as a static function in the Interface.cc translation unit rather
> than a member function:
>
> static bool isInRange(UINT idx, UINT max) { return idx < max; }


[snip]
As to the interface, my reaction to your reaction is "what the hell
is wrong with a non-static member?"


It seems to pollute the interface. It's good to do error checking, but
why should this be an explicit part of the class definition? (Note my
self-reply that this class will only be used as a leaf.)
...


Hm... I don't understand how are you planning to implement it as a
'static' function when it needs access to the 'd_maxidx' member of a
concrete object. Are you suggesting that 'd_maxidx' should also be
pulled out of 'Interface' and made a 'static' variable?

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #5

"David Rubin" <bo***********@nomail.com> wrote in message
news:3F***************@nomail.com...
This is a question related to my previous post titled "private member
functions:"

I recently saw that a colleage of mine submitted code which defines a
class Interface as follows[1]:

class Interface {
public:
Interface(UINT maxidx);

void set(UINT idx, UCHAR data);
/* ... */
private:
UINT d_maxidx;

bool isInRange(UINT idx) { return idx < d_maxidx; }
};

My first reaction to this code is that isInRange should have been
defined as a static function in the Interface.cc translation unit rather
than a member function:

static bool isInRange(UINT idx, UINT max) { return idx < max; }

Is my reaction correct? Any opinions?


I think the point is you'd have to know what max is, and what if you don't?
In other words, there could be multiple instances of class Interface lying
around at any point in time. How are you going to keep track of the max
value for all of them? Isn't it better to let each instance keep track of
this number, without you having to do that work? That is, after all, one of
the basic principles of OO programming. There's nothing wrong with a static
function per se, although I have to question whether or not you really need
an Interface class to tell you if one number is bigger than another number.
Jul 19 '05 #6

"David Rubin" <bo***********@nomail.com> wrote in message
news:3F***************@nomail.com...

It seems to pollute the interface. It's good to do error checking, but
why should this be an explicit part of the class definition?


Because the max number is an explicit part of the class definition, which is
exactly where it belongs. A class is data along with the functions that
operate on that data, encapsulated. If the data can't be static, then why
should be function be? I'm not saying there should never be static
functions, but they are usually best for when they don't need any data that
is specific to any one instance of the class.
Jul 19 '05 #7
Andrey Tarasevich wrote:

David Rubin wrote:
> class Interface {
> public:
> Interface(UINT maxidx);
>
> int set(UINT idx, UCHAR data); [change! ^^^] > /* ... */
> private:
> UINT d_maxidx;
>
> bool isInRange(UINT idx) { return idx < d_maxidx; }
> }; > My first reaction to this code is that isInRange should have been
> defined as a static function in the Interface.cc translation unit rather
> than a member function: > static bool isInRange(UINT idx, UINT max) { return idx < max; }

[snip] Hm... I don't understand how are you planning to implement it as a
'static' function when it needs access to the 'd_maxidx' member of a
concrete object. Are you suggesting that 'd_maxidx' should also be
pulled out of 'Interface' and made a 'static' variable?


I was imagining something like this:

/* Interface.cc */

static bool isInRange(UINT idx, UINT max) { return idx < max; }

int Interface::set(UINT idx, UCHAR data)
{
int r = ERROR;

if(isInRange(idx, d_maxidx)){
/* non-trivial code involving data */
r = OK;
}
return r;
}

isInRange does not appear in the Interface header file at all. This way,
a proliferation of validation functions will not affect the class
interface; they are merely implementation details. The fact that
Interface::set, for example, returns a (documented) error condition is
enough to suggest to implementors that some sort of validation must be
done. However, I'm conflicted about imposing the details on Interface
clients.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #8
jeffc wrote:
My first reaction to this code is that isInRange should have been
defined as a static function in the Interface.cc translation unit rather
than a member function:

[snip] I think the point is you'd have to know what max is, and what if you don't?
In other words, there could be multiple instances of class Interface lying
around at any point in time. How are you going to keep track of the max
value for all of them? Isn't it better to let each instance keep track of
this number, without you having to do that work? That is, after all, one of
the basic principles of OO programming. There's nothing wrong with a static
function per se, although I have to question whether or not you really need
an Interface class to tell you if one number is bigger than another number.


Most people seem to have misread my original comment, quoted above. I am
talking about a static function in a translation unit, not a class
function (declared as static). I think my response to Andrey Tarasevich
(elsethread) explains the point a little more clearly.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #9

"David Rubin" <bo***********@nomail.com> wrote in message
news:3F***************@nomail.com...
jeffc wrote:
My first reaction to this code is that isInRange should have been
defined as a static function in the Interface.cc translation unit rather than a member function:
[snip]
I think the point is you'd have to know what max is, and what if you
don't? In other words, there could be multiple instances of class Interface lying around at any point in time. How are you going to keep track of the max
value for all of them? Isn't it better to let each instance keep track of this number, without you having to do that work? That is, after all, one of the basic principles of OO programming. There's nothing wrong with a static function per se, although I have to question whether or not you really need an Interface class to tell you if one number is bigger than another

number.
Most people seem to have misread my original comment, quoted above. I am
talking about a static function in a translation unit, not a class
function (declared as static). I think my response to Andrey Tarasevich
(elsethread) explains the point a little more clearly.


I didn't know that, but even so, my advice still stands, except for the
second half of the last sentence.
Jul 19 '05 #10
David Rubin wrote:


Most people seem to have misread my original comment, quoted above. I am
talking about a static function in a translation unit, not a class
function (declared as static). I think my response to Andrey Tarasevich
(elsethread) explains the point a little more clearly.


I'd say that this was an implementation detail that ought to
be kept out of the class header.

I suspect that it is being used to check the 'idx' value
being passed to set(). Being private clients are unable to
use it to check that 'idx' is ok or not.

I suppose that UINT is some unsigned integer - do you really
want a 4Gb range, wont 2Gb suffice? In general 'unsigned' is
a nuisence because you can't check if the value is negative
or not, and the compiler will silently promote an int to
unsigned:

int main()
{
int i = -1;
unsigned ui = i;
return 0;
}

wont generate a warning on some compilers.

More importantly this isInRange method is an example of what
I was talking about in your post about private members
functions. It has utility outside of the class, particularly
if it takes an upper and lower value like:

bool isInRange(int value, int min_value, int max_value) {
return value >= min_value && value <= max_value;
}

you might want to only use greater than and less than though.

The point being is that often these 'private methods' have a
utility outside of the class they were originally written for.

Jul 19 '05 #11

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

Similar topics

5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
2
by: Test User | last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people as possible) I am somewhat familiar with Access 2000, but my latest project has me stumped. So, I defer to you...
6
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...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
17
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
6
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...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.