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

Re^3: perltidy block indentation

by Tux (Canon)
on Dec 04, 2013 at 16:55 UTC ( [id://1065615]=note: print w/replies, xml ) Need Help??


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

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1065615]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-19 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found