Fail Fast
| class CruiseControl { |
| static final double SPEED_OF_LIGHT_KMH = 1079252850; |
| static final double SPEED_LIMIT = SPEED_OF_LIGHT_KMH; |
| |
| private double targetSpeedKmh; |
| |
| void setTargetSpeedKmh(double speedKmh) { |
» | if (speedKmh < 0) { |
| throw new IllegalArgumentException(); |
| } else if (speedKmh <= SPEED_LIMIT) { |
| targetSpeedKmh = speedKmh; |
» | } else { |
| throw new IllegalArgumentException(); |
| } |
| } |
| } |
Let’s take a look at the method setTargetSpeedKmh(). The if conditional with its three branches surely does some exception handling. But do you immediately understand why it’s structured that way? Probably not.
The normal path in this method is a little bit concealed. It’s the second of the three ...
Get Java By Comparison now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.