Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2010/08/18

The Heartbreak of Copy/Paste Code

This is actual CGI code that I'm working on.

    if ( 0 ) { }    # BLANK to make adding and changing elsifs easier
    elsif (    defined $cgi->param( 'request_id' )
            && defined $cgi->param( 'status' ) ) {
        my @keys ;
        my @vals ;
        my $data ;
        $data->{ request_id } = $cgi->param( 'request_id' ) ;
        $data->{ status }     = $cgi->param( 'status' ) ;
        $data->{ program }    = $cgi->param( 'program' ) ;
        $data->{ notes }      = $cgi->param( 'notes' ) ;
        create_new_request_status $data ;

        my $request_id = $cgi->param( 'request_id' ) ;
        $output .= $cgi->h2( 'Request #' . $request_id ) ;
        $output .= $cgi->div( show_request( $request_id ) ) ;
        }
    elsif (    defined $cgi->param( 'request_id' )
            && defined $cgi->param( 'barcode' ) ) {
        my $data ;
        $data->{ request_id } = $cgi->param( 'request_id' ) ;
        $data->{ val }        = $cgi->param( 'barcode' ) ;
        my $err        = update_request_barcode( $data ) ;
        my $request_id = $cgi->param( 'request_id' ) ;
        $output .= $cgi->h2( 'Request #' . $request_id ) ;
        $output .= $cgi->div( show_request( $request_id ) ) ;
        }
    elsif (    defined $cgi->param( 'request_id' )
            && defined $cgi->param( 'request_name' ) ) {
        my $data ;
        $data->{ request_id } = $cgi->param( 'request_id' ) ;
        $data->{ val }        = $cgi->param( 'request_name' ) ;
        my $err        = update_request_name( $data ) ;
        my $request_id = $cgi->param( 'request_id' ) ;
        $output .= $cgi->h2( 'Request #' . $request_id ) ;
        $output .= $cgi->div( show_request( $request_id ) ) ;
        }
    elsif (    defined $cgi->param( 'request_id' )
            && defined $cgi->param( 'species' ) ) {
        my $data ;
        $data->{ request_id } = $cgi->param( 'request_id' ) ;
        $data->{ val }        = $cgi->param( 'species' ) ;
        my $err        = update_request_species( $data ) ;
        my $request_id = $cgi->param( 'request_id' ) ;
        $output .= $cgi->h2( 'Request #' . $request_id ) ;
        $output .= $cgi->div( show_request( $request_id ) ) ;
        }
    elsif (    defined $cgi->param( 'request_id' )
            && defined $cgi->param( 'source' ) ) {
        my $data ;
        $data->{ request_id } = $cgi->param( 'request_id' ) ;
        $data->{ val }        = $cgi->param( 'source' ) ;
        my $err        = update_request_source( $data ) ;
        my $request_id = $cgi->param( 'request_id' ) ;
        $output .= $cgi->h2( 'Request #' . $request_id ) ;
        $output .= $cgi->div( show_request( $request_id ) ) ;
        }
    elsif (    defined $cgi->param( 'request_id' )
            && defined $cgi->param( 'library_type' ) ) {
        my $data ;
        $data->{ request_id } = $cgi->param( 'request_id' ) ;
        $data->{ val }        = $cgi->param( 'library_type' ) ;
        my $err        = update_request_library_type( $data ) ;
        my $request_id = $cgi->param( 'request_id' ) ;
        $output .= $cgi->h2( 'Request #' . $request_id ) ;
        $output .= $cgi->div( show_request( $request_id ) ) ;
        }
    elsif (    defined $cgi->param( 'request_id' )
            && defined $cgi->param( 'lab_director' ) ) {
        my $data ;
        $data->{ request_id } = $cgi->param( 'request_id' ) ;
        $data->{ val }        = $cgi->param( 'lab_director' ) ;
        my $err        = update_request_lab_director( $data ) ;
        my $request_id = $cgi->param( 'request_id' ) ;
        $output .= $cgi->h2( 'Request #' . $request_id ) ;
        $output .= $cgi->div( show_request( $request_id ) ) ;
        }
    elsif ( defined $cgi->param( 'request_id' ) ) {
        my $request_id = $cgi->param( 'request_id' ) ;
        $output .= $cgi->h2( 'Request #' . $request_id ) ;
        $output .= $cgi->div( show_request( $request_id ) ) ;
        }
There's value-scrubbing on the other side of all the update_request_* stuff. At one point, there were four cases here, and now there's nine. The if (0) {} bit at front was there to make it easy for me to juggle order if I want, but the rest ....

I'm Dave, and I'm a Copy/Paste Coder.

If I was tossing to the same function at the end, it would be one thing. I can imagine having something like the following function to fix things, but the big elsif chunk in the center seems only marginally better.
sub un_copy-paste {
    my $output ;
    my ( $request_id , $key , $val ) = @_ ;
    $data->{ request_id } = $cgi->param( 'request_id' ) ;
    $data->{ val }        = $cgi->param( 'source' ) ;
    my $err ;
    if ( 0 ) {}
    elsif ( $key eq 'source' ) { update_request_source( $data ) ; }
    elsif ( $key eq 'species' ) { update_request_species( $data ) ; }
    # and more like this
    $output .= $cgi->h2( 'Request #' . $request_id ) ;
    $output .= $cgi->div( show_request( $request_id ) ) ;
    return $output ;
    }
I think reading more Higher Order Perl could help me come up with something non-stupid.

5 comments:

  1. Make the elsif a trinary table, as shown below? (or a given/when if you're requiring or running on 5.10 or above?)

    ('species' eq $key) ? update_request_species($data) :
    ('source' eq $key) ? update_request_source($data) :
    ();

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I posted some code but it looked like crap, so I made it a gist at github

    It uses a hash of coderefs, which I think is the most straightforward solution. It also uses curried coderefs, which is a little weird, but kind of like HOP. I love coderefs :P

    ReplyDelete
  4. Thanks for the ideas. My Wednesdays are busy, so while I tossed my physical copy of HOP into my bag and my PDF copy into my Dropbox, I didn't read a word of it.

    ReplyDelete
  5. I eventually had to add a table for the names of fields in the input hash and the names of the params that fit into it, but now I'm special-case-less and free!

    ReplyDelete