Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: perltidy block indentation

by dasgar (Deacon)
on Dec 04, 2013 at 15:07 UTC ( #1065603=note: print w/ replies, xml ) Need Help??


in reply to Re: perltidy block indentation
in thread perltidy block indentation

there should *NEVER* be an else after return, exit or die

I'm not trying to say that you're wrong, but I'm personally struggling to understand the logic/rationale behind your claim. Based on the responses from Bloodnok and marto, there appears to be a good valid reason for doing as you are suggesting. I guess I haven't learned enough about Perl (and/or its internals) to understand what this reason is. Can you help me by providing more info on the rationale behind your suggestion so that I (and possibly others) can better understand your suggestion?

Based on my current levels of knowledge and experience with Perl, I see the OP's code and your suggested code as being logically equivalent -- as in, both codes produce the same result. If I were responsible for maintaining the code, I would prefer the OP's code because I personally find it easier to read and follow and it will be less likely to cause me to misread what the code is doing.

Again, I'm not trying to challenge you on this. I'm just trying to understand the logic and reasoning behind your view on what I would describe as a "best practice".


Comment on Re^2: perltidy block indentation
Select or Download Code
Re^3: perltidy block indentation
by Tux (Monsignor) on Dec 04, 2013 at 16:55 UTC

    I'll try

    sub start_working_day { if ($me->is_sick) { return undef; } elsif ($wheather->raining) { $travel->use_car; } else { $travel->use_bike; } $travel->go; $work->do; return; }

    As you can see in this snippet, there are several expressions that control the start of a working day. After the if/elsif/else the real work starts, as it is the same for all situations: it is common code. Or is it? No it is not! When I am ill, the common code is never executed, so there is no reason whatsoever to have an else block: there is no need for an alternative, as there is no intent to run that common code.

    My stance is that a function or method should be written in a way that the most likely codepath is the easiest to follow with the eye. Exceptions should exit early.

    sub start_working_day { $me->is_sick and return undef; if ($wheather->raining) { $travel->use_car; } else { $travel->use_bike; } $travel->go; $work->do; return; }

    As you can now deduce, the code after the if/else is run for all cases of the decision tree, and hence reflects the thinking logic.


    Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-12-20 16:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (96 votes), past polls