in reply to Efficiency & Maintenance: if/elsif or Other Dispatch Method

You could bring your limits and calculated rv's into an array of arrays, and then iterate through it to test outcomes. See below:

use strict; use warnings; my @outcome_tests = ( [ 97, 100, '99' ], [ 93, 97, '95' ], [ 85, 93, '90' ], [ 75, 85, '80' ], [ 65, 75, '70' ], [ 55, 65, '60' ], [ 45, 55, '50' ], [ 35, 45, '40' ], [ 25, 35, '30' ], [ 15, 25, '20' ], [ 7, 15, '10' ], [ 3, 7, '05' ], [ 0, 3, '00' ], ); for( 1 .. 20 ) { # Run the test 20 times. my $outcome = int( rand( 100 ) + 1 ); # Generate some test data. my $rv = ''; TEST: { foreach my $test ( @outcome_tests ) { my( $lower, $upper, $category ) = @{ $test }; if( $outcome > $lower and $outcome <= $upper ) { $rv .= $category; last TEST; } } die "ERROR: $outcome is out of bounds.\n"; } print "$outcome => $rv\n"; }

One advantage to this solution is that it turns your limits and categories into data, rather than code. You can add or remove limit pairs without altering the code, and without affecting complexity.


Dave