Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Is there a name for secondary or derived properties?

Reply
Thread Tools

Is there a name for secondary or derived properties?

 
 
fishfry
Guest
Posts: n/a
 
      07-07-2005
This question came up in my mind recently as I'm working with the XMLTV
package, which retrieves an XML document representing a television
schedule. To work with it I've created a Program object, which has
attributes such as "start_time" and "channel" and "title" and so forth.

In addition, I'd like to know which programs are movies, so I have an
attribute called isMovie. However, isMovie is not a fundamental
attribute of the program data as given to me by the XML. Instead, I
determine whether a program is a movie based on other criteria, in
particular whether it has a star_rating defined (like "*" or "**" etc.)

As I was coding this, I thought of a theoretical question. I have
accessor methods such as start_time() and channel() that get and set the
start_time and channel attributes. I also have an isMovie() method that
computes whether the program is a movie or not and returns a boolean.

I was noticing that isMovie() is conceptually different than accessors
that hide "primary" attributes, if you want to call them that. For
example, if a user calls isMovie() on a program object whose star-rating
has not been set yet, then isMovie() will return false, which is an
inaccurate result. In fact, at that moment the Program object is really
in an "incomplete" state, and the result of isMovie() is really
indeterminate.

To do this correctly, the documentation should inform the caller that
isMovie() must not be called until the Program object is completely
initialized ... but how would they know when that is? Perhaps isMovie()
should return true, false, or undefined and the documentation should
explain what that means. And if you were implementing this concept in a
language like C that does not have a concept of "undefined," you'd have
to return -1, 0, and 1 and document that.

I was wondering if there is some computer science term for this concept
of primary versus derived attribute of an object, and if there is a
recommended way to handle it.

By the way this is how I implemented my code:

In sub new:

$self = bless {
_start_time => '',
_starRating => undef,
<other stuff>,
_isMovie => sub {return defined($self->{'_starRating'});},
}, $class;

and later ...

sub isMovie {
my($self) = shift;
return $self->{'_isMovie'}->();
}

Is this a reasonable way to do this kind of thing? I don't think I've
ever felt the need to use an anonmous subroutine before, but in this
instance it felt natural.

Thanks for all input.
 
Reply With Quote
 
 
 
 
Fabian Pilkowski
Guest
Posts: n/a
 
      07-07-2005
* fishfry schrieb:

> For
> example, if a user calls isMovie() on a program object whose star-rating
> has not been set yet, then isMovie() will return false, which is an
> inaccurate result. In fact, at that moment the Program object is really
> in an "incomplete" state, and the result of isMovie() is really
> indeterminate.
>
> To do this correctly, the documentation should inform the caller that
> isMovie() must not be called until the Program object is completely
> initialized ... but how would they know when that is?


To do this correctly your constructor should expect some parameters for
initialization. Therewith you avoid such an indeterminate state of your
object. IMHO it's not really pretty to give someone an almost empty
object and force him to set its attributes afterwards. Do it the other
way round: Create your object after you have all informations you need
to avoid such problems.

>
> I was wondering if there is some computer science term for this concept
> of primary versus derived attribute of an object, and if there is a
> recommended way to handle it.


I don't know a term for that (which doesn't mean that there is no one).
But I would implement both kinds of attributes identically, without any
anonymous subs.

>
> sub isMovie {
> my($self) = shift;
> return $self->{'_isMovie'}->();
> }
>
> Is this a reasonable way to do this kind of thing? I don't think I've
> ever felt the need to use an anonmous subroutine before, but in this
> instance it felt natural.


I would write it as

sub isMovie {
my $self = shift;
return defined $self->{_starRating} ? 1 : 0;
}

This way, the information that "isMovie" depends on "_starRating" is
clear when looking at this sub. Otherwise you have to look at the
constructor to find this out. And IMHO it's unusual to hide such info in
the constructor. Btw, from the programmers point of view there is
absolutely no difference between isMovie() and other methods (you called
"primary" ones).

regards,
fabian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Is there anyway to delete a secondary oe mail account when the pw is way beyond memory (mine) Camera Computer Security 7 08-30-2003 10:35 AM



Advertisments