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