This is an AMP version of the article, its original content can be found here.

If. Then. Throw. Else. WTF?

This is the code I could never understand:

I have been trying to find a proper metaphor to explain its incorrectness. Today I finally found it.

If-then-else is a forking mechanism of procedural programming. The CPU either goes to the left and then does something or goes to the right and does something else. Imagine yourself driving a car and seeing this sign:

It looks logical, doesn't it? You can go in the left lane if you're not driving a truck. Otherwise you should go in the right lane. Both lanes meet up in a while. No matter which one you choose, you will end up on the same road. This is what this code block does:

Now, try to imagine this sign:

It looks very strange to me, and you will never see this sign anywhere simply because a dead end means an end, a full stop, a finish. What is the point of drawing a lane after the dead end sign? There is no point.

This is how a proper sign would look:

This is how a proper code block would look:

The same is true for loops. This is wrong:

While this is right:

There is no road after the dead end! If you draw it, your code looks like this very funny snippet I found a few years ago reviewing sources written by some very well-paid developer in one very serious company:

Don't do this.