Is there a name for secondary or derived properties?

Discussion in 'Perl Misc' started by fishfry, Jul 7, 2005.

  1. fishfry

    fishfry Guest

    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.
    fishfry, Jul 7, 2005
    #1
    1. Advertising

  2. * 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
    Fabian Pilkowski, Jul 7, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Morten Wennevik
    Replies:
    3
    Views:
    392
    =?Utf-8?B?U3JlZWppdGggUmFt?=
    Nov 2, 2005
  2. =?Utf-8?B?R2FyeQ==?=
    Replies:
    1
    Views:
    507
    =?Utf-8?B?R2FyeQ==?=
    Nov 11, 2005
  3. Christopher Clark

    Secondary background problem

    Christopher Clark, Mar 28, 2005, in forum: HTML
    Replies:
    4
    Views:
    412
    Blinky the Shark
    Mar 28, 2005
  4. Replies:
    1
    Views:
    393
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    385
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page