in reply to efficient use of subroutines
Should I make a new level of subroutines to have less repeating code? (In other words, my subroutines would have subroutines.)Sure, don't hesitate. Whether in Perl or in other languages, I regularly have four or five levels of subroutine calls (and sometimes even much more).
The main reason for making subs is obviously to remove code duplication. If you find yourself copying and pasting a dozen or more lines of code, then those lines should probably go into a sub. Often even for less than a dozen lines.
Sometimes, subroutines are good even if they are called only once, because they hide the gory details from the main stream of thought. One of the litmus tests to figure out if it makes sense to make a sub is whether it is easy to give them a name that makes that sense. For example, I am often writing programs that need to compare data from different files. I might write something like this (untested code):
Each sub is used only once, but it removes the details about opening the file, reading the data, perhaps splitting it, loading data into the hash and perhaps closing the filehandler. But, of course, I am happier if I can use the same procedure for the two data loads:my $old_cust_data_href = load_old_cust_data($old_cust_data_file); my $new_cust_data_href = load_new_cust_data($new_cust_data_file); my $result_hash_ref = data_compare($old_cust_data_href, $new_cust_data +_href); # ...
Or possibly even better:my $old_cust_data_href = load_cust_data($old_cust_data_file); my $new_cust_data_href = load_cust_data($new_cust_data_file); my $result_hash_ref = data_compare($old_cust_data_href, $new_cust_data +_href); # ...
Although there might be some exceptions, a good subroutine is often one that qualifies as a "pure function", i.e. a subroutine that, given the call parameters, return the same results (no sideboard effect, no global variable).my $cust_data_href{$_} = load_cust_data($cust_data_file{$_}) for qw /o +ld new/; my $result_hash_ref = data_compare($cust_data_href{old}, $cust_data_hr +ef{new});
Then, of course, there is the danger of getting too far. I remember having worked on a huge mass of C++ code where just about every single method had no more than two lines of code, so that you ended up with a messy spaghetti of method calls. Going to that extreme makes thing very difficult to maintain, what I sometimes call write-only code.
Update: modified the last code sample which was not fully consistent.
|
|---|