Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Style: expr or warn vs. if..warn?

by FoxtrotUniform (Prior)
on Jul 09, 2004 at 22:43 UTC ( #373282=perlquestion: print w/replies, xml ) Need Help??

FoxtrotUniform has asked for the wisdom of the Perl Monks concerning the following question:

So I'm writing some code to perfect-shuffle two lists, and I want it to complain when it doesn't get lists of equal length. Which style is preferred?

# .. or warn is homomorphic to the .. or die idiom, and is # phrased like an assert. @foo == @bar or warn "perfect_shuffle: lists must have equal length\n"; # explicit if makes adding cleanup easier, and is phrased # like an error check. if(@foo != @bar) { warn "perfect_shuffle: lists must have equal length\n": }

Edit: s/scalar \@/\@/g;; thanks adrianh!

F o x t r o t U n i f o r m
Found a typo in this node? /msg me
% man 3 strfry

Replies are listed 'Best First'.
Re: Style: expr or warn vs. if..warn?
by hossman (Prior) on Jul 09, 2004 at 23:19 UTC

    A good rule of them i like to apply, is use "if (cond){ block }" only if block is more then one statement.

    As for using "A or B" vs "B unless A" .. I like to make the "main" action be the first one, so that people skimming your code get an idea of what you are doing without reading hte whole line. But in the examples you provide, deciding what the "main" action is depends largely on your perception of the greater context...

    • Are you primarily warning about bad input, and the size comparison is a condition of that warning?
    • Are you primarily comparing the size, and the warning is potential result of the comparison?

    Given such a short bit of code, it probably doesn't matter much. However, if you were doing a big chunk of input/data validation, with lots of different warnings in differnt cases, I'd use...

    warn "blah blah" unless A; warn "yak yak" unless B; warn "bafkjlkajaksjdf;lkjas;kaj;aksdf;kjalkjas;dkf" unless C; warn "blblblblblbblblblblbl" unless (D or E);

    if you're doing a lot of processing, and the warning is just an afterthough in one section of hte processing, then i'd use or...

    doSomething(); something->else(); third_thing() or warn "something didn't work quite right"; still(processing);
Re: Style: expr or warn vs. if..warn?
by blokhead (Monsignor) on Jul 09, 2004 at 23:52 UTC
    I used to use "this if that" and "this unless that" a lot until very recently. Without even making a conscious effort, I've been using short-circuit "and"/"or" in my last few projects. And now that I look over some recent code, I seem to be using quite an inconsistent melange of the two. Yikes! I'll have to nail this down and make a rule for myself...

    I like this rule-of-thumb: Use if/unless for checking a condition. Use and/or when the statement does something with side-effects.

    do_something_complex() or die; do_something_complex() and then_do_something_else(); # condition checking: die "terribly" if $condition; die "terribly" unless $obj->can("foo");
    Clearly there's a bit of overlap between "checking a condition" and "doing something with side-effects." In the event of a tie, I wholeheartedly agree with hossman that the most important part of the statement should be at the beginning, because it's easier to skim the code.

    Also, don't forget to consider the surrounding code. If you have a bunch of guard statements in a row that try to do/check something or else die/return, make them all the same structure!


Re: Style: expr or warn vs. if..warn?
by adrianh (Chancellor) on Jul 09, 2004 at 22:51 UTC

    The calls to scalar are redundant, and I'd probably do:

    warn "perfect_shuffle: lists must have equal length\n" unless @foo == @bar;
Re: Style: expr or warn vs. if..warn?
by Old_Gray_Bear (Bishop) on Jul 09, 2004 at 23:19 UTC
    Opinion -- both are equally clear at first glance.

    I personally don't like the "Action unless Condition" form, it's too easy to eye-skip over the the second clause, partiiculary if the 'unless' is on the next line. I know that the complier/interpreter will fold the various constructs into an single "action if condition" form; but I tend to look at the condition as the more important part of the statement, so I prefer it first. Tastes Differ.

    I Go Back to Sleep, Now.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://373282]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2020-10-24 01:35 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (242 votes). Check out past polls.