http://www.perlmonks.org?node_id=912467

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

Hi!

I was wondering if I will get any erratic results if I do not use 'else' at the end of 'if-elsif-else' statement. I have the following script, which seems to be working file. My input file is very large, I do not want to put an 'else' statement at the end. Is this ok? Thanks.

#!/usr/bin/perl use strict; use warnings; use 5.010; open (TRACK1, ">", "track1.txt") or die "Can't open track1.txt : + $!"; open (TRACK2, ">", "track2.txt") or die "Can't open track2.txt : + $!"; open (TRACK3, ">", "track32.txt") or die "Can't open track3.txt +: $!"; while (<>) { if(/name="track1/ .. /^1_000/){ print TRACK1; } elsif(/name="track2/ .. /^5_000/) { print TRACK2; } elsif(/name="track3/ .. /^20_000/) { print TRACK3; } #no 'else' here. }

Replies are listed 'Best First'.
Re: if-elsif-else (?)
by AnomalousMonk (Archbishop) on Jul 02, 2011 at 15:32 UTC

    What you are saying by omitting an else-clause from an if or if-elsif statement is that either it cannot possibly happen or it does not matter that the conditional test or tests all fail. If this is true, you need no else-clause.

    If the conditional tests can all fail and this makes a difference, then you need an else-clause.

Re: if-elsif-else (?)
by GrandFather (Saint) on Jul 02, 2011 at 22:59 UTC

    Absolutely omit an "unused or useless" else clause. Including it adds clutter to your code making it harder to find the code that does real work and making it harder to see the decision tree that gets you to any particular outcome.

    With that in mind there is no need to add extra comments to say "Oh, and I really mean not to have some code here". Do you put a comment at the end of each line to say "This line is not conditional on anything"? The code structure itself should reflect thee intended behaviour. Relying on comments to make that clear will simply lead to trouble when the implementation and comment drift apart - perl doesn't read your comments.

    That said, it may be that a better implementation for your code could be something like:

    #!/usr/bin/perl use strict; use warnings; use 5.010; my @tracks = map {open my $track, ">", $_ or die "Can't open $_: $!"; + $track} 'track1.txt', 'track2.txt', 'track3.txt'; for my $track (@tracks){ print <$track>; }

    or if the "line" numbers are important:

    #!/usr/bin/perl use strict; use warnings; use 5.010; my @tracks = (['track1.txt', 1000], ['track2.txt', 5000], ['track3.txt', 20000] +); for my $track (@tracks) { open my $trackFile, ">", $track->[0] or die "Can't open $track->[0 +]: $!"; $track = [$trackFile, $track->[1]]; } for my $track (@tracks) { my ($trackFile, $lines) = @$track; print <$trackFile>; }

    Whenever you catch yourself doing xx1 = ...; xx2 = ...; ... think instead about arrays and loops. It makes the code clearer and much much easier to maintain.

    True laziness is hard work
Re: if-elsif-else (?)
by Albannach (Monsignor) on Jul 02, 2011 at 16:04 UTC
    Just to add to the other responses, because the else is usually used, I would consider it good practice to at least have a comment explaining that it is deliberately omitted and why. I've commonly seen the else included but with no statements, just to show to future maintainers that it is not an error but by design. It may also be worthwhile to add a logging call in the else so it won't appear to your user, but it might provide useful information to you on what real-world data looks like. You may think now that you know all possible input cases, but the world has a way of changing things without telling you. At least my world does!

    --
    I'd like to be able to assign to an luser

Re: if-elsif-else (?)
by Anonymous Monk on Jul 02, 2011 at 15:04 UTC

    I was wondering if I will get any erratic results if I do not use 'else' at the end of 'if-elsif-else' statement.

    No. An else is not required, and the absence of else won't have any affect

Re: if-elsif-else (?)
by sundialsvc4 (Abbot) on Jul 02, 2011 at 19:07 UTC

    To my way of thinking, the absence of an else clause is something that I would not expect to see ... hence, that I might well mis-read, especially in the heat of the moment of trying to troubleshoot a bug in your unfamiliar-to-me code.   It’s kinda like this very old saw:

    PARIS IN THE THE SPRING
    (“Paris in the spring,” right?   Look again.)

    So, I would end the statement this way:

    else { # (do nothing ...) };

    The Perl compiler obviously won’t care either way, but the colleague who follows me (or I, myself, if more than just a little bit of time has passed ...) will appreciate the familiar clarity.   I have not only made the code conform to what your eyes expect to see, but also I have clearly spelled out the designer’s intentions.   Now, there is no question about, “is that a tpyo?”   The meaning is now crystal-clear.

    JM2CW ...

Re: if-elsif-else (?)
by JavaFan (Canon) on Jul 03, 2011 at 20:31 UTC
    My input file is very large, I do not want to put an 'else' statement at the end. Is this ok?
    You have the code already written. What made you think asking random strangers on the internet gives you a faster and better answer than just running your code and finding out?