From b92b7d088362bf587194a2538fac50fdf8435aa3 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Thu, 24 Nov 2016 02:12:29 +0100 Subject: [PATCH] Thoughts about Feature Envy and path to follow when refactoring legacy code 1. When writing tests for legacy code we go from shortest to deepest branches. 2. When refactoring legacy code (handmade modifications) we go from deepest to shortest branches (just the opposite) Feauture envy: some class (TripService) wants to do some action that should be implemented by another class which we are using to extract the data and perform some action. Data and action should be done by the same class (User) --- .../java/org/craftedsw/tripservicekata/trip/TripService.java | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 3eec3a8..570b6a0 100644 --- a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java @@ -17,12 +17,22 @@ public class TripService { User loggedUser = getLoggedInUser(); boolean isFriend = false; if (loggedUser != null) { + // Feature envy. TripService envies User class. This should be done by User class. for (User friend : user.getFriends()) { if (friend.equals(loggedUser)) { isFriend = true; break; } } + // 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 (isFriend) { tripList = tripsBy(user); } -- 2.1.4