Is there a name for secondary or derived properties?

F

fishfry

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.
 
F

Fabian Pilkowski

* fishfry said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top