Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^4: Useless use of string in return statement

by eyepopslikeamosquito (Archbishop)
on Apr 13, 2021 at 23:39 UTC ( [id://11131233]=note: print w/replies, xml ) Need Help??


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

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. :)

Replies are listed 'Best First'.
Re^5: Useless use of string in return statement
by Bod (Parson) on Apr 14, 2021 at 11:23 UTC
    In case you're interested

    Always interested in learning alternative approaches...

Re^5: Useless use of string in return statement
by Bod (Parson) on Apr 14, 2021 at 11:30 UTC
    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://11131233]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-19 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found