From: Gustavo Martin Morcuende Date: Thu, 24 Nov 2016 23:31:02 +0000 (+0100) Subject: Moving Guard Condition to the top X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=cdcd5e2a1678a5ec9aed9cf2e4af964babfbf2e0;p=JavaForFun Moving Guard Condition to the top --- diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java index 57d1f42..264f2cd 100644 --- a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java @@ -10,28 +10,16 @@ import org.craftedsw.tripservicekata.user.UserSession; public class TripService { public List getTripsByUser(User user) throws UserNotLoggedInException { - List tripList = new ArrayList(); - // In Unit Test we shouldn't invoke other classes because - // other classes could be using data base, network, etc, etc. - // User loggedUser = UserSession.getInstance().getLoggedUser(); User loggedInUser = getLoggedInUser(); - if (loggedInUser != null) { - // The deepest branch. For refactoring legacy code we must begin from the - // deepest branch. This is just the opposite for creating the unit test - // for our legacy code. - // 1. Write the unit test from the shortest to deepest branch. Modifications - // in legacy code must be done JUST with the automatic tools provided by the IDE. - // 2. Once the legacy code is under test start refactoring from deepest - // to shortest branch. Modifications in the legacy code may be hand made. - // In this case we can not do anything with this code (this is the deepest branch) - // so we will have to start with the for loop (which is the second deepest branch) - if (user.isFriendsWith(loggedInUser)) { - tripList = tripsBy(user); - } - return tripList; - } else { + if (loggedInUser == null) { throw new UserNotLoggedInException(); } + + List tripList = new ArrayList(); + if (user.isFriendsWith(loggedInUser)) { + tripList = tripsBy(user); + } + return tripList; } protected List tripsBy(User user) {