in reply to reuse worth it for small items?

I saw what some other people have done and to a certain degree they are missing the point of encapsulation and design.

  1. You want a subroutine that prevents redundancy, excellent
  2. There is only one variable changed in the request
  3. There is only one variable changed from the result
  4. Keep your code focused, make it simple and do not do too many things at one time
sub get_rows_from { my $dbh = shift; my $table = shift; $dbh->Sql ("SELECT * FROM " . $table); my %result; while ($dbh->FetchRow() ) { my %x= $dbh->DataHash(); $result{ $x{ID} } = \%x; } return \%result; } my $config_data_ref = $dbh->get_rows_from( 'ConfigData' ); my $dicom_data_ref = $dbh->get_rows_from( 'DicomDestinations' );

So what we have a subroutine, with a simple interface. We only have to pass one thing to it (the table name). We will get back a hash reference. Since we are not doing data manipulation in the subroutine, we do not have to guess what the result will be if we passed a hash reference to the subroutine.

Replies are listed 'Best First'.
Re^2: reuse worth it for small items?
by John M. Dlugosz (Monsignor) on May 08, 2006 at 15:25 UTC
    I think you made a good point: one of the changed things is a parameter, but the other is a result and should be handled differently.

    I'd thought about making it a function or a loop body with the other concepts mentioned on this thread: pairs, or a hash mapping the name to the variable. That is enlightening: thanks.

    —John