This is a mobile version, full one is here.
Yegor Bugayenko
21 January 2015
If. Then. Throw. Else. WTF?
This is the code I could never understand:
if (x < 0) {
throw new Exception("X can't be negative");
} else {
System.out.println("X is positive or zero");
}
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:
if (x < 0) {
System.out.println("X is negative");
} else {
System.out.println("X is positive or zero");
}
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:
if (x < 0) {
throw new Exception("X can't be negative");
}
System.out.println("X is positive or zero");
The same is true for loops. This is wrong:
for (int x : numbers) {
if (x < 0) {
continue;
} else {
System.out.println("found positive number");
}
}
While this is right:
for (int x : numbers) {
if (x < 0) {
continue;
}
System.out.println("found positive number");
}
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:
if (x < 0) {
throw new Exception("X is negative");
System.exit(1);
}
Don’t do this.