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

Re: Conditional style (if, &&) query

by petdance (Parson)
on Oct 17, 2001 at 08:04 UTC ( [id://119326]=note: print w/replies, xml ) Need Help??


in reply to Conditional style (if, &&) query

Everything depends entirely on the specific context. That's the beauty of TMTOWTDI.

I usually try to sound as English-like as possible, and keep in mind what I'm trying to emphasize: the test or the action? There's no One Right Way, even within the same program or even the same block of code. Go with what makes the most sense for the specific piece of code.

Also, consider the surrounding code. Something as simple as conditionally printing a phone number can depend greatly on why it's doing it. The "standard" way might be:

if ( $obj->show_phone ) { $obj->print_phone_number; }
Here's some code where that probably is best:
# Validate the phone according to basic U.S. rules if ( $phone =~ s/^(1-)?(\d\d\d-?\d\d\d-?\d\d\d\d)$/ ) { $phone = $2; $phone =~ s/-//g; # Strip dashes $obj->set_phone( $2 ); } else { $obj->error( "Invalid phone number" ); } if ( $obj->show_phone ) { $obj->print_phone_number; }
We check the phone number, and make sure it's a valid format, and then when we're done, we might print the phone number. Whether we print or not is incidental to the rest of the block.

But consider this instance where the context is entirely different.

$obj->print_name; $obj->print_address; $obj->print_phone_number if $self->showphone; $obj->print_email_address;

Here, it's more appropriate to show that we're printing a bunch of information, and the phone number is optional. Writing it the "old way", with the phone number block taking three lines, would break up the visual flow, like so:

$obj->print_name; $obj->print_address; if ( $obj->show_phone ) { $self->print_phone_number; } $obj->print_email_address;
Blech.

I think rule two of Perl, after TMTOWTDI, should be "It all depends."

xoxo,
Andy
--
<megaphone> Throw down the gun and tiara and come out of the float! </megaphone>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found