Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: Useless use of string in return statement

by Bod (Parson)
on Apr 13, 2021 at 15:09 UTC ( [id://11131189]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Useless use of string in return statement
in thread Useless use of string in return statement

Use block if, not postfix if

Are you suggesting postfix if should never be used?

I agree that adding an extra conditional statement requires more work and more consideration but there are cases, like my conditional return statement, where it is rather unlikely that any extra statements would want to be added to the conditional execution.

Replies are listed 'Best First'.
Re^4: Useless use of string in return statement
by Your Mother (Archbishop) on Apr 13, 2021 at 15:13 UTC

    I believe TheDamian suggested it in the excellent style guide PBP. It’s one of the few suggestions in the book I oppose. Post-fix conditionals are, to me, extremely natural language. If kept terse and indented properly, they make code more readable.

      It's a book that has sat on my bookshelf for many, many years. I did once delve into it and concluded it was beyond me. Having increased my understanding and exposure to Perl quite a bit since then, I have moved it from bookshelf to desk and shall take another look...

Re^4: Useless use of string in return statement
by haukex (Archbishop) on Apr 13, 2021 at 15:21 UTC
    Are you suggesting postfix if should never be used?

    My rule of thumb is Statement Modifiers should only be used on things that really only are a single statement, not multiple things stuck together with the Comma Operator or a do BLOCK. (Update: To clarify: IMHO and TIMTOWTDI.)

      Someone, I believe it was Larry, said that the choice should be made such that the "most important part" of the statement comes first/leftmost. So in the case of error handling - such as the failure of open - using the modifier 'if' is appropriate.

        But what *is* the most important part with errors?

        die "I hate you" if $string =~ m/error/i;

        or

        $string =~ m/error/i and die "I hate you";

        As with most, it is a matter of taste and brain. *My* brain 100% prefers the second over the first. Note that I am not of native English tongue. That might matter.

        My rule of thumb is to forbid all postfix if/unless except when the expression is a simple $debug / $verbose >= $n.


        Enjoy, Have FUN! H.Merijn
Re^4: Useless use of string in return statement
by eyepopslikeamosquito (Archbishop) on Apr 13, 2021 at 23:39 UTC

    Are you suggesting postfix if should never be used?
    Yes. Well, I have happily never used it for fifteen years. Conway made a convincing argument IMHO. In case you're interested, I would write your:
    return encode_json(\%result) if lc($attrs{'format'}) eq 'json';
    as:
    lc($attrs{'format'}) eq 'json' and return encode_json(\%result);
    ... which is similar in style to the classic:
    open(my $fh, '<', $file) or die "error opening '$file': $!";
    with flow of control on the right. I guess my brain has got used to scanning the right of the screen for flow of control.

    Curiously, the instant I glanced at your original code:

    return encode_json(\%result) if lc($attrs{'format'}) eq 'json'; return $result{'message'} or "$result{'api-key'}:$result{'session'}";
    my spidey sense went off. Ding! Ding! Ding! This reaction was pure instinct, far too quick to be based on any rational analysis. Every atom in my body screamed at me that this code is wrong! It was morning in Sydney Bod, so looking at your code woke me up nicely and got me going for the day. Thanks for that BTW. :)

      In case you're interested

      Always interested in learning alternative approaches...

      looking at your code woke me up nicely and got me going for the day. Thanks for that BTW. :)

      I try to wake up to coffee but each to their own 😜

      In fact, I wake up to audiobooks. The only bit of real, useful code I've written in Java* is an alarm app for my mobile. It plays an audio book picking a random book and working through it every morning. If a chapter is too long it skips to the next after three mornings. It also stops playing after 20 minutes (or whatever I set it to) because if it kept going, I would listen to the book rather than getting up and starting my day!

      * only because I haven't found a way to run Perl code on Android.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-18 18:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found