in reply to Refactoring nested if block
It's hazardous to your health to refactor code without test data, but I've asked a similar question in the past, so here's my attempt.
I'm not sure about the math in the condition if( @cur_spin == ( $row_length + 15 ) ) {, which I why I left it there rather than folding it into the constant at the top. It seems to me that every time you use the $row_length constant, you then add 15 more items to @cur_spin anyway. So factoring that out, by adding 15 to the constant and always add the 15 items reduces the clutter--but I'd need to test it to convince myself that I did it correctly.
Copying all the data to a temp array and then having to reference the slice [ 3.. 17 ] in 3 different places seems clumsy. If you need to use the temp array, better to only copy the slice you are going to need, and then use it all each time. Of course that would translate the magic [3] into the magic [ 0 ].
Two locally scoped variables called $major_frame_index & $minor_frame_index represent multiple instances of 12 redundant characters, long, ugly lines and no gain in clarity.
You seem to be uncomfortable with using @array in a scalar context as the size of the array, which is quite common, but how could the size of the array ever be negative in my $cur_spin_cnt = (scalar @cur_spin > 0) ? scalar @cur_spin : 0?
Update: Added the missing first line that went walkabout when C&Ping.
sub WriteSpinData { my( $OUT_FH, $ref ) = @_; my( $last_major, $line, @cur_spin ); my( $row_length ) = 15*160+1; for my $filename ( sort keys %{ $ref } ) { for my $major ( sort keys %{ $ref->{$filename} } ) { for my $minor ( grep{/^\d+$/} keys %{ $ref->{$filename}{$m +ajor} } ){ @cur_spin = "$filename,$major,$minor" if $ref->{$filename}{$major}{$minor}[ 3 ] == 165 and ( $minor % 10 ) == 0; push @cur_spin, @{ $ref->{$filename}{$major}{$minor} } +[ 3..17 ]; if( @cur_spin == ( $row_length + 15 ) ) { print $OUT_FH join( ',', @cur_spin ), "\n"; @cur_spin = (); } } $last_major = $major; } printf "%s: %d major frames\n", basename( $filename ), $last_m +ajor; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Refactoring nested if block
by bowei_99 (Friar) on Mar 25, 2006 at 09:14 UTC | |
by xdg (Monsignor) on Mar 25, 2006 at 13:30 UTC | |
by BrowserUk (Patriarch) on Mar 25, 2006 at 10:16 UTC | |
by Argel (Prior) on Mar 28, 2006 at 00:51 UTC | |
by BrowserUk (Patriarch) on Mar 28, 2006 at 09:04 UTC |