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.