473,324 Members | 2,248 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,324 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 3287
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,...
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
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.