Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: perltidy block indentation

by dasgar (Priest)
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".

Replies are listed 'Best First'.
Re^3: perltidy block indentation
by Tux (Abbot) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1065603]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-05-21 21:50 GMT
Find Nodes?
    Voting Booth?