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

is it bad form to leave off the else in if statements if you are not going to use it? just something i'm pondering..
like this-
if ($x = $y) { print "$y = $x\n"; }
instead of-
if ($x = $y) { print "$y = $x\n"; } else { #waste of space }

Replies are listed 'Best First'.
RE: a question on style
by Ovid (Cardinal) on Jun 12, 2000 at 20:30 UTC
    Two comments:
    1. Leave off the else if you don't use it. Including unused code is usually begging for confusion later on.
    2. Are you sure your code above is correct? You have an assignment operator "=" instead of a logical operator like "==" or "eq".
    The second point is significant (and if it was just a typo, I'll complete this thought for others because I've tripped on this a time or two).

    Consider the following code:

    $x = 3; $y = "Ovid"; if ($x = $y) { print "This is true\n"; } else { print "This is false\n"; }
    This code will always print "This is true" because the "=" assigns the value of $y to $x (in this case, both wind up with the value of "Ovid"). The if statement winds up evaluating the true/false value of "Ovid" and concludes because it is not undef and not zero, it must be true and the statement prints. Therefore, the above code probably doesn't behave the way you want it to.

    Interestingly, the following snippet will work the way you expect it, but not for the reasons that are immediately obvious:

    $x = 3; $y = 0; if ($x = $y) { print "This is true\n"; } else { print "This is false\n"; }
    This code will print "This is false", but not because $x failed to equal $y. In this case, $y is assigned to $x and the if statement evaluated the resulting value, in this case '0' (zero) and zero evaluates as false.

    Always remember when using an if statement or other conditional that you usually want to test equality with a logical operator such as "==" for numerics and "eq" for non-numerics.

      thanx, i know the difference between = and == that code is just a quick sample to illustrate my point, it serves no functionality
        After I got through about half of my reply, I thought to check your home node and realized that you had more XP than I and I suspected that I was teaching my grandpa how to suck eggs (you can just ignore that comment if you ain't from the South). I almost scratched the post, but figured that I have tripped so many times on that little problem that I figured it might be worth rehashing to others. Sorry if it was a bit pedantic.

        Hey, what do other monks think? Was I explaining something that was too obvious, like "here's how to add two numbers together"? I wouldn't mind getting a feel for that. I don't want to waste bandwidth if everyone feels they are way beyond that.

Re: a question on style
by lhoward (Vicar) on Jun 12, 2000 at 18:41 UTC
    IMHO, it is preferable to omit a useless else. I've never seen anyone recommend including useless elses unless the code was under development and you were goning to put something in the else at a later date.
    if($x==$y){ print "good return\n"; }else{ # put in error handling code here }
RE: a question on style
by nuance (Hermit) on Jun 12, 2000 at 18:43 UTC

    It is not bad form to leave out the else if you are not using it. In fact I would say that the opposite is the case, adding an unused else is merely cluttering the code.

    I would consider you second exapmle to be a mistake, but that's just my opinion.

    If you type perlman:perlsyn into the search box It'll bring up the syntax documentation. As you can see the if statement with no else is the first quoted example i.e. it is good style


Re: a question on style
by cleen (Pilgrim) on Jun 13, 2000 at 04:00 UTC
    Of course thats not bad, and if your really going for the non-cluttered, more style code
    if ($x = $y) { print "$y = $x\n"; }
    might look better as
    print "$y = $x\n" if ($x = $y);
    which is basically the same thing, but easyier to type and read.

    but I always look at if/ifels/else statements as a good chance to put in some nifty debugging options, IE
    if ($y eq $x) { print "$y = $x\n"; } elsif ($debug eq 1) { print "$y != $x\n"; }
    but thats just my two cents.