Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: else after a return, exit, croak, die

by GrandFather (Saint)
on Jan 25, 2023 at 02:05 UTC ( [id://11149856]=note: print w/replies, xml ) Need Help??


in reply to else after a return, exit, croak, die

I presume he was reacting to:

if (@ARGV) { if (@ARGV == 1) { Download($ARGV[0]); EXIT(0, 'SUCCESS. Argument was received from command line.'); } else { PrintUsage(); EXIT(-1, 'Argument missing.'); } } else { Download(GetArgs()); EXIT(0, 'SUCCESS. Argument was received from stdin.'); }

That can be rewritten as:

if (@ARGV) { if (@ARGV == 1) { Download($ARGV[0]); EXIT(0, 'SUCCESS. Argument was received from command line.'); } PrintUsage(); EXIT(-1, 'Argument missing.'); } Download(GetArgs()); EXIT(0, 'SUCCESS. Argument was received from stdin.');

which reduces the number of lines of code and makes the logic clearer. Even better would be:

use strict; use warnings; if (!@ARGV) { Download(GetArgs()); EXIT(0, 'SUCCESS. Argument was received from stdin.'); } if (@ARGV == 1) { Download($ARGV[0]); EXIT(0, 'SUCCESS. Argument was received from command line.'); } PrintUsage(); EXIT(-1, 'Argument missing.');

which avoids nested code paths altogether. Wherever the code path can't continue out of a previous block there is no need for an else. That avoids nesting following code and generally makes the logic easier to follow.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-25 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found