Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
1. There shall NEVER be an else after a return/exit/croak/die! NEVER!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: else after a return, exit, croak, die
by GrandFather (Saint) on Jan 25, 2023 at 02:05 UTC | |
I presume he was reacting to:
That can be rewritten as:
which reduces the number of lines of code and makes the logic clearer. Even better would be:
which avoids nested code paths altogether. Wherever the code path can't continue out of a previous block there is no need for an else. That avoids nesting following code and generally makes the logic easier to follow.
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
| [reply] [d/l] [select] |
Re: else after a return, exit, croak, die
by Tux (Canon) on Jan 25, 2023 at 09:05 UTC | |
See here for a short explanation. Also see this lightning talk. Click on the title for next page. Enjoy, Have FUN! H.Merijn | [reply] |
Re: else after a return, exit, croak, die
by davido (Cardinal) on Jan 25, 2023 at 17:18 UTC | |
That may be taken out of context. I don't know the actual context; it's not linked to. But it could be a reference to Structured Programming, championed by Dijkstra. See https://softwareengineering.stackexchange.com/a/118760/24692. Dave | [reply] |
Re: else after a return, exit, croak, die
by harangzsolt33 (Deacon) on Jan 25, 2023 at 03:17 UTC | |
In this situation, everything after the return statement in the sub will never run. So, it's dead code. It's a good idea to eliminate dead code from a program. Also, consider this example:
In this for loop, it appears at first that we count from zero to 1999. But that's not the case. We exit out of the loop when $i == 1024, so you might as well remove that if statement and just make the for loop shorter. This is true assuming that other things in the loop aren't going to be affected by this. And in this case, it's perfectly safe. Now, let's look at the if () else structure combined with return statements:
The problem here is that the "else" statement is unnecessary, because this would accomplish the same thing as this code:
This code could be even further simplified to:
See? We started out with 8 lines of code, and we reduced it to just one line. Now, this is not always a good idea, because if shortening the code causes it to become harder to understand, then it may not be a good idea. But usually if you can write shorter code that does the exact same thing, then the shorter is better. Here is an example where shorter code makes things harder to understand. Example A and B and C all do the exact same thing, but example B and C are harder to read. And being able to read code is important too:
If instead of $A $B and $C we could use much longer variable names, you would see that example A becomes much much longer, but it is still easier to read, so I think that is better (unless your goal is to write code that no one understands). lol We can write something in fewer keystrokes, but it would just become more complicated. GrandFather's signature "Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond" has a lot of truth in it. ;) Consider this: 5-10 years go by and you open your perl script to read your own code. And if you write like in example A, you will understand it immediately. But if you code like in example B or C, you will be pulling your hair out. lol | [reply] [d/l] [select] |