Cut and paste another branch when you need it

People get all upset about long if-elsif chains, but if they’d just use a proper code editor with cut-n-paste and search, it’s really not all that bad. With split panes, multiple windows, and a big monitor, I can see hundreds of lines of code at once, so what’s your problem? Only got 24 rows? Get with the times, weirdo! Get a bigger monitor! Or maybe get some glasses if you can’t read the font size.

if( $hash{caseA} ) {
    some_sub( $hash{caseA} );
} elsif ( $hash{caseA} ) {
    some_sub( $hash{caseA} );
} elsif ( $hash{caseB} ) {
    some_sub( $hash{caseB} );
} elsif ( $hash{caseC} ) {
    some_sub( $hash{caseC} );
} elsif ( $hash{caseD} ) {
    some_sub( $hash{caseD} );
} elsif ( $hash{caseE} ) {
    some_sub( $hash{caseE} );
} elsif ( $hash{caseF} ) {
    some_sub( $hash{caseF} );
} elsif ( $hash{caseG} ) {
    some_sub( $hash{caseG} );
} elsif ( $hash{caseH} ) {
    some_sub( $hash{caseH} );
} elsif ( $hash{caseI} ) {
    some_sub( $hash{caseI} );
} elsif ( $hash{caseJ} ) {
    some_sub( $hash{caseJ} );
} elsif ( $hash{caseK} ) {
    some_sub( $hash{caseK} );
} elsif ( $hash{caseL} ) {
    some_sub( $hash{caseL} );
} elsif ( $hash{caseM} ) {
    some_sub( $hash{caseM} );
} elsif ( $hash{caseN} ) {
    some_sub( $hash{caseN} );
} elsif ( $hash{caseO} ) {
    some_sub( $hash{caseO} );
} elsif ( $hash{caseP} ) {
    some_sub( $hash{caseP} );
} elsif ( $hash{caseQ} ) {
    some_sub( $hash{caseQ} );
} elsif ( $hash{caseR} ) {
    some_sub( $hash{caseR} );
} elsif ( $hash{caseS} ) {
    some_sub( $hash{caseS} );
} elsif ( $hash{caseT} ) {
    some_sub( $hash{caseT} );
} elsif ( $hash{caseU} ) {
    some_sub( $hash{caseU} );
} elsif ( $hash{caseV} ) {
    some_sub( $hash{caseV} );
} elsif ( $hash{caseW} ) {
    some_sub( $hash{caseW} );
} elsif ( $hash{caseX} ) {
    some_sub( $hash{caseX} );
} elsif ( $hash{caseY} ) {
    some_sub( $hash{caseY} );
} elsif ( $hash{caseZ} ) {
    some_sub( $hash{caseZ} );
} else {
    print "No way!\n"
}

I could use something like a dispatch table or refactor the code, but that’s really a lot more work than adding another branch every time I have a new case, even if it does act like all of the other cases. No one has complained about it so far, so don’t rock the boat, you know? Besides, I probably won’t have to look at the code for another six months, so it’s someone else’s problem. Let them figure it out if they care so much about it. If they were so hot in Perl they’d just write some more Perl to generate that code.

print "if( \$hash\{caseA\} ) {\n";
print "    some_sub( \$hash\{caseA\} );\n";

foreach ( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ) {
    $a = uc $_;
    print "\} elsif ( \$hash\{case$a\} ) {\n";
    print "    some_sub( \$hash\{case$a\} )\;\n";
}

print "\} else \{\n";	
print "    print \"No way\!\\n\"\n";
print "\}\n";
Leave a comment

0 Comments.

Leave a Reply

You must be logged in to post a comment.