March 2018
Intermediate to advanced
208 pages
4h 52m
English
| | class BoardComputer { |
| | |
| | CruiseControl cruiseControl; |
| | |
| | void authorize(User user) { |
| | Objects.requireNonNull(user); |
| » | if (user.isUnknown()) |
| | cruiseControl.logUnauthorizedAccessAttempt(); |
| » | if (user.isAstronaut()) |
| | cruiseControl.grantAccess(user); |
| » | if (user.isCommander()) |
| | cruiseControl.grantAccess(user); |
| | cruiseControl.grantAdminAccess(user); |
| | } |
| | } |
Here we’ve translated the switch statement from the previous comparison into separate if statements. There’s one problem, though. The indentation in the snippet is perilously misleading. There are no curly braces after the if, so the condition applies only to the subsequent line. That makes the whole method behave maliciously—the line cruiseControl.grantAdminAccess(user); ...