private Boolean driveCar() {
if (isGasTank.isEmpty()) {
nextLocation = findNearestGasStation();
if (driver.isHungry()) {
driver.buyChipsAndSoda();
if (driver.isFeelingLucky()) {
if (coinToss() == HEADS) {
driver.buyLotteryTicket(LUCKY_NUMBERS);
} else {
driver.buyLotteryTicket(QUICK_PICKS);
}
}
}
} else {
keepDriving();
}
}Oooooh, doesn't that look pretty when all the indentation is lined up so nicely and it makes sideways triangles from whitespace? Some people really like that. But what is even prettier to me is readable code. By the time the code gets 3+ levels deep it's a refactoring opportunity.
Here's some "low hanging fruit" I ran across just recently that will get fixed before the next release:
if (this.isActive()) {
doSomething();
} else {
if (somethingElse.isUseful()) {
// do something useful
}
}Yeah, it's a convenient way of increasing your nesting by putting your if inside of your else. I'm not sure what else this accomplishes other than increasing nesting. To be fair, maybe there used to be more logic tucked inside there and it wasn't needed anymore. When that happens though, I just want developers to clean up after themselves. I didn't look back in the revision history to see if this was a valid excuse (or to see if I wrote it!). But really, I shouldn't have to come up with excuses.
Okay, one more piece of fruit, maybe you'll like this one better:
Person pers = new Person("Don","Draper");
if (pers != null) {
somethingUseful(pers);
}Do you see it? Yes, immediately after instantiating a new object we check if it's null. (It adds to the nesting, too.) I've never tried to write a constructor that would return null -- maybe it's possible. Whoever wrote this (hopefully it wasn't me) really doesn't trust the author of the Person class. :)
What pointless code have you seen recently?
0 comments:
Post a Comment