--- Matija Papec <[EMAIL PROTECTED]> wrote:
> I'm curious which of the two examples is more preferred or it depends
> entirely on someone style? 
> ----------------
> $x = 1;
> 
> #1)
> if ($x) { $x = 0 }
> #2)
> $x = 0 if $x;
> ----------------
> IMO first is more convenient and orthodox but that is just me. :)

Assuming this is just example code (not much point to it otherwise, =o)
then it really doesn't matter much. The parser will likely boil it all
down to the same thing anyway.

When it matters is when the visual appearance of your script is notably
altered. Sometimes the postfix is neater -- personally, I don't like
opening and closing a block on the same line in an if, but that's just
me... and I *do* do it when I have a conditional else, and both are
just one line.

   if ($x) { y() } else { z() }

though usually they're long enough that I drop them to seperate lines.

   if    ($x) { x() }
   elsif ($y) { y() }
   else       { z() }

I don't like that structure, either, and often rewrite it with a
trinary:

   $x ? x() :
   $y ? y() :
   $z ? z() ;

But what counts most (aside from making it do what you want) is that
you can read it, that you can read it and edit it without a headache in
six months or a year, and that the next guy can read it and edit it
without having to come beat you up. =o)

I like postfix conditions whenever I can use them, but I like them best
when I have a complex condition that has optional parts:

   if ($x) {
      x();       # doesn't execute unless $x
      y() if $y; # doesn't execute unless $x *AND* $y
   }

y() is ideal for a postfix, because all other conditions have already
been tested; this keeps the program from suffering excessive
indentation caused by another block of curlies, though with such a
simple statement as this you could still put the curlies on the same
line, as you did above.

So, to answer your question, it depends largely on the code your
writing, but mostly on someone's style.

=====
print "Just another Perl Hacker\n"; # edited for readability =o)
=============================================================
Real friends are those whom, when you inconvenience them, are bothered less by it than 
you are. -- me. =o) 
=============================================================
"There are trivial truths and there are great Truths.
 The opposite of a trival truth is obviously false.
 The opposite of a great Truth is also true."  -- Neils Bohr

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

Reply via email to