Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Designing a package object

by PhiRatE (Monk)
on Jul 25, 2002 at 05:47 UTC ( [id://185127]=note: print w/replies, xml ) Need Help??


in reply to Designing a package object

Others have mentioned the die() error, and I believe you have already solved the HoH problem but I'll go into detail about a "theoretical" problem in addition:

Complex interfaces

The problem primarily is that your set_choices interface takes a complex datatype. This gives you very little room for in-module syntax checking. You can either reject the entire set of choices and leave the interface-programmer wondering where they went wrong, or not check it at all and end up with the interface-programmer scratching their head at what is presented to them on the screen.

Rather than taking a HoH as a single argument to the set_choices() function, you should instead repeatedly call an add_choice() function or equivalent to add choices. Yes, its a little more work, but the advantage is that you can do nice checks against the data as it comes in without having to traverse the complex type itself, you can also (but not in this instance) use perl prototyping to help the interface-programmer get their code right at compile time.

The most important benefit however is documentation simplicity. It is trivial to document the following sub, whereas documenting set_choices() above would require being very specific about the nature of each of the entries, the format required, plus the duplication of _id and the hash key (which is also $id).

sub set_choice { my ($self, $id, $name, $sub_or_id, $return_sub) = @_; $id !~ /^\d+$/ && die "ID must be a valid positive number"; $name || die "Name must be specified"; $self->{$id}{_id} = $id; $self->{$id}{_name} = $name; $self->{$id}{_sub_or_id} = $sub_or_id; $self->{$id}{_return_sub} = $return_sub; }

This also means that the interface-programmer can store their menu entires in whatever form is convenient to them, without having to do the (possibly error-prone) translation to your format before submission, ie:

my @entries = ("Entry #1","Entry #2","Entry #3","Quit"); my $count=0; for (@entries) { $menu->set_choice($count++,$_); }

Which may be considerably more convenient for them, especially if the menu items are coming out of a database or part of a larger data structure.

Always try to keep the interfaces simple, its the point at which the interface-programmer and the module-programmers minds have to mesh, and thus the weakest point in the application.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://185127]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found