Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Refactoring just to refactor?

by karlgoethebier (Abbot)
on Jun 28, 2019 at 12:43 UTC ( #11102079=note: print w/replies, xml ) Need Help??

in reply to Refactoring just to refactor?

I hope that i'm not totally Perl-Blind but a good refactoring might be to rewrite your fancy sub like this:

sub index_menu { my $dir = shift; # my @file_list = file_list('.'); my @file_list = file_list($dir); # if this is what you meant my $files = [ map { anchor( textify($_), { href => $_ } ) } grep { /^[A-Z].+/ && -f $_ } sort { article_sort($a, $b) } @file_list ]; return $files; }

You may also consider to use the more hard-boiled style and omit the explicit return but that's a matter of taste.

And if i where in your shoes i would leave my old working code untouched and use the BLOCK LIST style (which is the natural way IMHO) from today on.

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Replies are listed 'Best First'.
Re^2: Refactoring just to refactor?
by Lady_Aleena (Curate) on Jun 28, 2019 at 22:40 UTC

    Thank you for catching that I did not use $dir in the sub where I meant to use it.

    I am not clear on what you mean by "hard-boiled style". I always either return or print at the end of a sub. (I have not gotten to use the feature say that much.) I do not know how else to end a subroutine.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena

      A subroutine will return to its caller the result of the last evaluated expression. I would recommend to use return() in all cases within all branches, unless you know why/when not to.

      Here's an example of output of values. Note that say() is just a glorified print(), but it adds a newline at the end by default. Nothing special here...

      use warnings; use strict; use feature 'say'; sub return_print { my $x = 254; print "$x\n"; } sub return_return { my $x = 255; return $x; } say "print: " . return_print(); say "return: " . return_return();


      254 print: 1 return: 255

      So, the first output (254) is the output from the print() call. The second output (print: 1) is the output of a successful call to print(), which is 1. The third value is the return value explicitly sent via return().

      I would highly recommend you play around and get a grasp on what return values you're getting from and/or expecting from your function/method calls.

      All of your tests should be checking against all possible and potential return values so there aren't any unexpected results. If there are any odd things found, add more tests, or fix your subroutines to ensure they only return very specific value(s).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11102079]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2020-11-29 21:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found